473,503 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ifstream seeking

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
3 7210
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13745
by: Dave Johnston | last post by:
Hi, I'm currently trying to create a wrapper that uses C functions but behaves like ifstream (from fstream.h) - this is because the platform I'm using (WinCE) doesn't support streams and this is...
6
3622
by: Herv? LEBAIL | last post by:
Hi everybody, I'm writing a program which use the <string>, <vector> and <ifstream> classes. Given an array of string, i.e vector<string> file_names, example : file_names = "file1.txt"...
6
3370
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
4
3721
by: hall | last post by:
Hi. I ran across a bug in one of my problems and after spending some time tracking it down i found that the problem arose in a piece of code that essentially did this: ----------- ifstream...
2
2210
by: Mathieu Malaterre | last post by:
Hi, I am tryig to convert part of a c++ IO library. Right now I am removing all 'FILE*' and replace them with ifstream. But I have a problem, after seeking through a stream I have to call the...
5
6014
by: Jacek Dziedzic | last post by:
Hi! Consider the following program #include <fstream> #include <iostream> using namespace std; int main() { ifstream in("test.txt");
10
17882
by: sam | last post by:
Hi, Can anyone tell me how to print a file name from ifstream? the following cout code does not print the filename I created with ifstream preivous: ifstream is; is.open ("text.txt");
1
4459
by: Vadim Volevach | last post by:
Hi, // Under C++Builder 2007 (+ Dinkumware) #include <fstream> using namespace std; int main (int argc, char* argv) { // Create a DOS-Text file, that has only two lines: // - the first...
11
8099
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.