I want to read a .txt file in C# but I will not read all the lines at the same time. For example, consider 500 lines of text file. I want a function to run 25 times and read 20 consecutive lines each time. In the first call of function, lines from 1 to 20 will be read, and second time it is called, 21-40 will be read.
Below simple code does this in c++ but I don't know how to achieve it in C#:
string readLines(ifstream& i)
{
string totalLine="", line = "";
for(int i = 0; i < 20; i++){
getline(i, line);
totalLine += line;
}
return totalLine;
}
int main()
{
// ...
ifstream in;
in.open(filename.c_str());
while(true){
string next20 = readLines(in);
// so something with 20 lines.
}
// ...
}