Reputation: 376
I'd like to go to a specific character, in a specific line in my text file with fstream.
For exemple i want to go to line 2, i do this :
stream.ignore(20, '\n'); // because i have 20 characters in each line.
It works fine, but when i try to move in this line:
stream.seekg(1, ios::cur); // move 1 char from the current position.
And i test the output:
char test;
stream >> test;
It gives me a character in the middle of line 3 ! ( i am supposed to be on line 2)
And then i've checked if my current position is ok with :
stream.seekg(0, ios::cur);
stream >> char;
And it was ok, it gives me the char in the beginning of line 2. But as you see when i replace 0 by 1 it gives me a character in the middle of line 3 ! :/ Isn't supposed to move by 1 char ?
Do you have any idea why it does this ?
I am making a tile based isometric engine and i need to save and read my database in a text file. The area of the map which i am saving/reading needs to be on my view, so i have to read from a minx char to a maxx char, and miny(line) char to a maxy(line).
Thanks
Upvotes: 0
Views: 127
Reputation: 604
The link here: http://www.daniweb.com/software-development/cpp/threads/110602/problem-with-seekg states that the file has to be opened in binary mode for seekg to function reliably with offset. Try that.
Upvotes: 2
Reputation: 664
Try this:
cout<<"Now at "<<stream.tellg()<<endl;
stream.ignore(20,'\n');
cout<<"Moved to "<<stream.tellg()<<endl;
stream.seekg(1, ios::cur);
char test;
stream>>test;
cout<<"Got character "<<test<<" from position "<<stream.tellg()<<endl;
Upvotes: 1