By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,292 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,292 IT Pros & Developers. It's quick & easy.

ifstream seeking

P: n/a
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

Feb 15 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
toton wrote:
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
Feb 15 '07 #2

P: n/a
On Feb 15, 12:30 pm, John Harrison <john_androni...@hotmail.com>
wrote:
toton wrote:
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
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)

Feb 15 '07 #3

P: n/a
toton wrote:
On Feb 15, 12:30 pm, John Harrison <john_androni...@hotmail.com>
wrote:
>>toton wrote:
>>>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


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-lit...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
Feb 15 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.