Pls, help me: how can i do this:
I read a text file line-by-line using ifstream.getline();
How can i reposition the pointer so after i read for example 3 lines to start reading from the first line.
Thank you
Printable View
Pls, help me: how can i do this:
I read a text file line-by-line using ifstream.getline();
How can i reposition the pointer so after i read for example 3 lines to start reading from the first line.
Thank you
- open the file again
- store all the lines in an array, accses it after reading
Thank you for the reply but this is not my point, because i have thousands of lines, i don't want to store all the lines in memory. I wonder how can i close the stream or the file and open it again for processing lines from the begining.Quote:
Originally Posted by ElastoManiac
any ideea?
What about seekg()?
Step 1 - create the index
- use tellg() to find out where you are in the file
- read a line from the file
- loop until end of file.
Step 2 - use the index
- use seekg() to goto a previous tellg() result
- read the line
i did like this and didn't works:Quote:
Originally Posted by Salem
Code:// ...
char s[200];
ifstream fin;
fin.open("C:\\in.txt");
do {
fin.seekg(0, ios::big);
// read a string from the keyboard...
while(fin.getline(str, 200)) {
// if the string is found then break;
}
}while(true); // suppose reading at infinity
> fin.seekg(0, ios::big);
I thought it was beg, not big?
Also, it needs to be INSIDE the loop with the getline.
Also, you need to store each answer tellg() returns.
What doesn't work?
- it doesn't compile
- it always reads the first line
- it crashes
Sorry: was beg, not big.Quote:
Originally Posted by Salem
The positioning don't work simply with seekg(...).
I will try to retrive with tellg...
Thank you very much for the reply.
Best regards!