Re: some more file problems
On Apr 23, 8:35 am, "Mark S." <markstar.n0s...@hotmail.com> wrote:
James Kanze wrote:
On Apr 19, 12:01 pm, "Mark S." <markstar.n0s...@hotmail.com> wrote:
Hmm, I still have some trouble with files:
Hmm, you overlooked one sentence in my previous posting:-).
You check the status of an input stream after every
operation.
I'm sorry, but I have read the posts two more times now but I
don't know which sentence you are referring to.
The sentence "In practice, when inputting, you're checking
the status after each input anyway;". You can't use the result
of input until you've verified that the input actually worked.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
string s;
ifstream in("E07.cpp");
What happens if the open in the constructor fails?
getline(in, s);
What happens if the getline fails (perhaps because the open
beforehand failed)?
So you mean additional error checking is necessary?
Yes. If the file wasn't present, or was empty, or didn't
contain a newline character, getline will fail. And the
contents of s will not be signficant.
What would be the standard way of doing this?
if ( in ) {
// succeeded...
} else {
// failed...
}
Usually, you'll want to read more than just one line, and would
write something like:
while ( std::getline( in, s ) ) {
// ...
}
The most frequent reason for failure is that you've reached the
end of file, at least in the case of getline. For formatting
input, the issue is more complicated, since you can also have
format errors (i.e. you're reading an int, and the input is
"abc"). You can distinguish to some degree why the operation
failed by checking individual status bits:
in.bad() A hardware error. Not much you can do about
it (and many applications just treat it as
end of file).
in.fail() && ! in.eof()
A format error in the input. Tell the user.
In addition, you might want to try to
resynchronize and continue, by clearing the
error ("in.clear()"), reading data until you
think you'r resynchronized, and trying to
continue.
in.fail() && in.eof()
No more data was present. (Usually. There
are a few special cases where a format error
can also result in this situation.)
cout << s << endl;
in.seekg(0,ios::beg); // (1) going back to beginning of file
getline(in, s); // works!
cout << s << endl;
in.close();
int i = 0, j = 0;
ifstream in2("E07.cpp");
while(getline(in2, s))
i++;
in2.seekg(0,ios::beg); // (2) does not work
while(getline(in2, s))
j++;
cout << j << endl; // j is still 0!
}
- Why does (1) work, but (2) doesn't???
Because in (2), you've reached the end of file. An
operation failed, the stream object memorizes the error
until you explicitly reset it.
More importantly, how do I get it to work without opening
the file a second time?
istream::clear.
Alright, I have added the line ( in.clear(); ). But sadly, the
result is the same. :-(
Where did you add it. If you added it before the seek, it
should work.
- But in case I do want to open a file, how can I open the same file
with the same identifier?
ifstream in("test.cpp");
in.close; // works
in.open // does not work
ifstream in("test.cpp"); // also doesn't work of course (doub=
le
declaration of "in")
You'd have to give the identifier a second time.
Sorry, I don't know what you mean by that.
Me neither. Either some context is missing, or I was thinking
of something else. If you want to open a file a second time:
std::ifstream in( "test.cpp" ) ;
// ...
in.close() ;
in.clear() ;
in.open( "test.cpp" ) ;
// ...
The clear is formally necessary according to the current
standard. It won't be necessary in the next version of the
standard, and I think that some current implementations don't
require it either. (It doesn't make sense. close, followed by
open, means you're starting over.)
Alternatively, you can use a different variable:
{
std::ifstream in( "test.cpp" ) ;
// ...
}
{
std::ifstream in( "test.cpp" ) ;
// ...
}
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34