ifstream seeking

T

toton

Hi,
I want to unread a few things while I am reading a file. One
solution I know is putback the characters read to the buffer.
But as I need frequent moving file pointer , to a few steps back, I
was thinking of seekg & tellg function. But it is not workings as I
expect....

here is the test code,
std::string word;
std::string value;
while(stream_){
std::ifstream::pos_type pos = stream_.tellg();///Mark the current
stream pos (2933)
stream_ >> word; ///read the word
std::ifstream::pos_type pos1 = stream_.tellg();///stream pos (2942)
good
if(word == ".PEN_DOWN"){
int y;
stream_>>y; /// read some int from stream (the next one was int here
209, thus successful).
stream_.seekg(pos); ///seek to previous pos i.e 2933
std::ifstream::pos_type pos2 = stream_.tellg();///once again
check the current pos. Yes it is 2933
std::string x;
stream_>>x;///now read the string I expect it to be .PEN_DOWN
again. but it is 198 :(
}
The content of the file at that portion is,

..SEGMENT WORD 0-5 OK "Zaadje"
..PEN_DOWN
209 1810
198 1812
198 1812
198 1813

Is this a problem with internal buffering or operator>> ? How can
otherwise I go to the previously marked position ?

abir
 
J

John Harrison

toton said:
Hi,
I want to unread a few things while I am reading a file. One
solution I know is putback the characters read to the buffer.
But as I need frequent moving file pointer , to a few steps back, I
was thinking of seekg & tellg function. But it is not workings as I
expect....

here is the test code,
std::string word;
std::string value;
while(stream_){
std::ifstream::pos_type pos = stream_.tellg();///Mark the current
stream pos (2933)
stream_ >> word; ///read the word
std::ifstream::pos_type pos1 = stream_.tellg();///stream pos (2942)
good
if(word == ".PEN_DOWN"){
int y;
stream_>>y; /// read some int from stream (the next one was int here
209, thus successful).
stream_.seekg(pos); ///seek to previous pos i.e 2933
std::ifstream::pos_type pos2 = stream_.tellg();///once again
check the current pos. Yes it is 2933
std::string x;
stream_>>x;///now read the string I expect it to be .PEN_DOWN
again. but it is 198 :(
}
The content of the file at that portion is,

.SEGMENT WORD 0-5 OK "Zaadje"
.PEN_DOWN
209 1810
198 1812
198 1812
198 1813

Is this a problem with internal buffering or operator>> ? How can
otherwise I go to the previously marked position ?

abir

I compiled your code and it worked for me. Either you have a bug in your
version of the STL or (much more likely) a bug somewhere else in your
code. Why not post a complete (and hopefully small) program that
illustrates this problem and someone will take a look.

john
 
T

toton

I compiled your code and it worked for me. Either you have a bug in your
version of the STL or (much more likely) a bug somewhere else in your
code. Why not post a complete (and hopefully small) program that
illustrates this problem and someone will take a look.

john

I checked it. It is working fine with some ascii file . But not
working with a file, which I belief is utf-8 / contains \n\r as new
line char or some other problem (Which In windows notepad shows some
unknown char for newline and makes a horrible display, shows fine in
gvim)
Again this works if I manually feed a few lines with strstream.
so may be the getline function, which I used in a few places creates
the problem. Not sure how to make it work with a \n or \r or both.
Using VS2003 .NET (7.1)
 
J

John Harrison

toton said:
I checked it. It is working fine with some ascii file . But not
working with a file, which I belief is utf-8 / contains \n\r as new
line char or some other problem (Which In windows notepad shows some
unknown char for newline and makes a horrible display, shows fine in
gvim)
Again this works if I manually feed a few lines with strstream.
so may be the getline function, which I used in a few places creates
the problem. Not sure how to make it work with a \n or \r or both.
Using VS2003 .NET (7.1)

There's a few possibilities here

1) Don't mix getline with operator>>, it only causes problems. The
problem being that it's hard to be sure whether operator>> does or
doesn't read the end of line, which has a big effect on how the next
getline operates.

2) Understand binary mode

http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.12

3) getline takes an optional third parameter, which is the end of line
character

getline(in_file, line, '#')

reads a 'line' using # as an end of line character.

That said if you are really reading utf-8 then I expect you'll end up
writing your own I/O routines that use binary mode and read the input
one byte at a time.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top