P
panig
how the program knows when a file is finsihed, mmm?
panig said:how the program knows when a file is finsihed, mmm?
When it is closed.
panig said:and then ?
panig said:how the program knows when a file is finsihed, mmm?
while ( mystream >> MyVar )
{
// process MyVar
}
mystream.close();
or
while (std::getline( mystream, mystring) )
{
// process mystring
}
mystream.close();
Roland Pibinger said:After close() check the stream state. Otherwise you don't know "when a
file is finsihed".
in message
Please explain this. After you call .close() don't we know that the file is
"finished"?
Roland said:But when .close() fails?
Roland said:But when .close() fails?
Daniel said:Can .close() fail? I think not.
Kai-Uwe Bux said:Daniel said:Can .close() fail? I think not.
The standard seems to allow close to fail [27.8.1.3/6]:
basic_filebuf<charT,traits>* close();
Effects: If is_open() == false, returns a null pointer. If a put area
exists, calls overflow(EOF) to flush characters. If the last virtual
member function called on *this (between underflow, overflow, seekoff,
and seekpos) was overflow then calls a_codecvt.unshift (possibly several
times) to determine a termination sequence, inserts those characters and
calls overflow(EOF) again. Finally it closes the file (??as if?? by
calling std::fclose(file)). If any of the calls to overflow or
std::fclose fails then close fails.
Returns: this on success, a null pointer otherwise.
Postcondition: is_open() == false.
So what is the robust way to close a file?
if ( file.is_open() ) {
if ( file.close() == 0 ) {
// what do we do here? [A]
}
}
Roland said:Better avoid iostreams for real work.
.close() returns void
Daniel said:Kai-Uwe Bux quoted the standard saying that close() returns either
'this' or NULL.
So which is it?
Kai-Uwe Bux said:Daniel said:Kai-Uwe Bux quoted the standard saying that close() returns either
'this' or NULL.
I quoted the close() method for the underlying buffer. The file stream has a
close() method, which calls the close() for the underlying buffer
[27.8.1.13/4]:
void close();
Effects: Calls rdbuf()->close() and, if that function returns false, calls
setstate(failbit)(27.4.4.3) (which may throw ios_base::failure).
As you can see, this one returns void but sets the failbit if it fails.
Daniel T. said:Kai-Uwe Bux said:Daniel said:(e-mail address removed) (Roland Pibinger) wrote:
On Wed, 15 Nov 2006 03:21:46 GMT, "Daniel T." wrote:
So what is the robust way to close a file?
if ( file.is_open() ) {
if ( file.close() == 0 ) {
.close() returns void
Kai-Uwe Bux quoted the standard saying that close() returns either
'this' or NULL.
I quoted the close() method for the underlying buffer. The file stream
has a
close() method, which calls the close() for the underlying buffer
[27.8.1.13/4]:
void close();
Effects: Calls rdbuf()->close() and, if that function returns false,
calls
setstate(failbit)(27.4.4.3) (which may throw ios_base::failure).
As you can see, this one returns void but sets the failbit if it fails.
But the function doesn't return a bool it returns a pointer or NULL. So
by "returns false" I can only assume that they mean "returns NULL".
Except that doesn't work either because close() returns NULL if the file
wasn't initially opened, thus NULL doesn't mean a failure to close in
that case. Or is my snippet above in the fstream's close() function and
it sets failbit if the file was both open and close returned NULL?
I'm still left wondering, what is one supposed to do with a file if
close fails?
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.