Hello.
My qustion is, in a text file, if i know how many lines are there, is it possible to set position to begining of a certain line?
The line length differs.
My idea was like, to first of all count how many lines are inside the file.
Then count bytes of each line, like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
fstream file("Binary.bin", ios::out);
int lines = 82;//Just for example. The amount of lines inside the file.
int line2 = 0, linecounter = 0, counter = 0, middle = lines/2;
int pos = 0;
char b;
file.get(b);
while(file)
{
if(line2 == middle)
{
pos = file.tellg();
file.seekp(0 , ios::cur);
//Should print out the word thats in the middle line.
cout << "Position: "<< endl;
line2 = 0;
break;
}
if(b == '/n')
{
line2++;
counter++;
}
else counter++;
file.get(b);
}
|
So basicly, if i had to find line 60 inside the file, i could know how many symbols are there till line 60, so that i can put the seekp after the lst symbol.
At least thats how i imagined it.
There must be an easier way to do this.
Also, how do i print out the line? I mean after ive set the position?