473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Checking ifstream state

Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not? I am reading from a character
based stream in binary mode.

Fraser.
Jul 22 '05 #1
5 2154
Fraser Ross wrote:

Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not?


No.
When a stream has gone into a fail state, it will refuse working until
you clear that fail state.
So once a read operation has failed all subsequent read operations will
also fail.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2
Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not? I am reading from a character
based stream in binary mode.


There is a big difference between "bad()", "fail()" and "good()". Bjarne
Stroustrup explains the stream state in his book:

(back translated from the german translation :-)

== snip
If good() or eof() is set, the previous input operations are a success.
If good() is set, the next input operation could by successful. If
good() isn't set, the next input operation will fail for sure. Each
input operation an a stream that isn't "good()" has no effect.
(...)
The difference between bad() and fail() is subtly. Is the state fail()
but not bad(), then it's possible that the stream is still usable and no
characters are lost. Is the stream state bad() the stream state is fully
undefined.
== endsnip

hope this will help

best regards
Tobias

Jul 22 '05 #3
In message <40******@news.greennet.net>, Fraser Ross
<fraserATmembers.v21.co.unitedkingdom@?.?.invali d> writes
Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not? I am reading from a character
based stream in binary mode.

Once the fail state is set, it remains set until you clear it explicitly
by calling clear(). It's not even cleared by closing the file and
opening another, which often catches the unwary.

--
Richard Herring
Jul 22 '05 #4
If I know correctly, there is also some exception support that can be
activated. In this case, you don't have to check the state after each
operation, but you can catch the exception in a try catch block...

Catalin

"Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote in message
news:40******@news.greennet.net...
Can I do several reading operations and then check fail() or do I need to
check fail() after every reading operation. Is it possible than a read
operation can set fail() and the next not? I am reading from a character
based stream in binary mode.

Fraser.

Jul 22 '05 #5

"Tobias Erbsland"
If good() or eof() is set, the previous input operations are a success.


Not necessarily. When extraction is done character-wise eof will be set
when an attempt is made to read past the last character and fail will be set
in conjunction.

Fraser.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
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...
2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
4
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...
3
by: sredd01 | last post by:
Hello, Please look the code below where I am reading the first 2,2,4 bytes from a binary file using two methods. I am getting a wierd (wrong) output with ifstream and memcpy method, but get the...
10
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");
6
by: Dave | last post by:
In .Net 2003 if a line, read from a text file is larger than a size parameter, the ifstream getline(buff, sze) put the file pointer to the EOF, so next peek() returns EOF. I saw this problem...
4
by: jared.oconnell | last post by:
Howdy, I am having a weird problem with ifstream. ifstream img; img.open(im_fn.c_str(),ios::in|ios::binary); char *x = new char; if(!img) { cerr << "Error: problem reading img" << endl;...
2
by: Assertor | last post by:
Hi, All. (VC++6.0) I found some strange thins when using getline() and seekg() of std::ifstream. After the file position of an open file was shift to the end of the file, seekg() did not...
7
by: Boltar | last post by:
Hi I'm using ifstream (which I hardly ever use) to read an ascii text file (containing numbers delimited by newlines) in a loop using something like: ifstream infile("filename") int value; ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.