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
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
lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu
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.Originally Posted by ElastoManiac
any ideea?
What about seekg()?
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i did like this and didn't works: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
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Sorry: was beg, not big.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!