This is a discussion on Reading text files within the C++ Programming forums, part of the General Programming Boards category; Pls, help me: how can i do this: I read a text file line-by-line using ifstream.getline(); How can i reposition ...

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

2. - open the file again
- store all the lines in an array, accses it after reading

3. Originally Posted by ElastoManiac
- 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.

any ideea?

5. 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

6. Originally Posted by Salem
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
i did like this and didn't works:
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

7. > 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

8. Originally Posted by Salem
> 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.
The positioning don't work simply with seekg(...).
I will try to retrive with tellg...

Thank you very much for the reply.
Best regards!