drjrm3
drjrm3

Reputation: 4718

what would make ios::fail() evaluate to 1?

i am trying to open a file with

27   string tline;
28   ifstream finp; // input file
29   ifstream dinp; // data files
30 
31   finp.open(argv[1]);
32 
33 
34   cout << "finp.good() = " << finp.good() << endl;
35   cout << "finp.bad() = " << finp.bad() << endl;
36   cout << "finp.fail() = " << finp.fail() << endl;

and i end up with output

finp.good() = 0
finp.bad() = 0
finp.fail() = 1

now, i cannot find any good documentation on what would cause this other than that it is an internal logic problem. what am i supposed to do to correct this?

if it helps, i am running on linux where i need to include both <cstring> and <cstdlib> while i do not have to do this when running on OSX. could this be the problem? if so, how do i correct it?

Upvotes: 0

Views: 891

Answers (2)

drjrm3
drjrm3

Reputation: 4718

forgive me, i made a simple mistake. when i ported my source files over to the linux system, i had ported over a script as well. i was confusing the executable with the script and the script had a filename hardcoded into it which was not in the directory.

basically, i was accidentally trying to read a file which was not there!

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490028

fail() will return 1 when you attempt a conversion and it fails. For example, if the next character in the file is something other than a digit, and you attempt to read an int, then the failbit will be set, and fail() will return 1. Any conversion attempted when you're already at the end of the file will also set the failbit.

fail() will also return 1 when/if the badbit is set. This is set to signal a serious problem with the file itself, not just an inability to read some particular piece of data from the file.

Upvotes: 2

Related Questions