By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,344 Members | 1,853 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,344 IT Pros & Developers. It's quick & easy.

finish a file

P: n/a
how the program knows when a file is finsihed, mmm?

Nov 12 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
"panig" <pp********************@yahoo.co.idwrote:
how the program knows when a file is finsihed, mmm?
When it is closed.

--
To send me email, put "sheltie" in the subject.
Nov 12 '06 #2

P: n/a
don't close it if processing isn't finished yet.
panig wrote:
how the program knows when a file is finsihed, mmm?
Nov 12 '06 #3

P: n/a
and then ?

Daniel T. wrote:
"panig" <pp********************@yahoo.co.idwrote:
how the program knows when a file is finsihed, mmm?

When it is closed.

--
To send me email, put "sheltie" in the subject.
Nov 12 '06 #4

P: n/a
panig wrote:
and then ?
What does your tutorial say about fstream? What examples does it show? Do
any of them have "eof" in them?

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Nov 13 '06 #5

P: n/a
"panig" <pp********************@yahoo.co.idwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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();
Nov 13 '06 #6

P: n/a
On Mon, 13 Nov 2006 08:10:15 -0800, "Jim Langston"
<ta*******@rocketmail.comwrote:
>"panig" wrote:
>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();
After close() check the stream state. Otherwise you don't know "when a
file is finsihed".
Nov 13 '06 #7

P: n/a
"Roland Pibinger" <rp*****@yahoo.comwrote in message
news:45***************@news.utanet.at...
On Mon, 13 Nov 2006 08:10:15 -0800, "Jim Langston"
<ta*******@rocketmail.comwrote:
>>"panig" wrote:
>>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();

After close() check the stream state. Otherwise you don't know "when a
file is finsihed".
Please explain this. After you call .close() don't we know that the file is
"finished"?
Nov 14 '06 #8

P: n/a
On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
>"Roland Pibinger" wrote in message
>After close() check the stream state. Otherwise you don't know "when a
file is finsihed".

Please explain this. After you call .close() don't we know that the file is
"finished"?
But when .close() fails?
Nov 14 '06 #9

P: n/a
Roland Pibinger wrote:
On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
>>"Roland Pibinger" wrote in message
>>After close() check the stream state. Otherwise you don't know "when a
file is finsihed".

Please explain this. After you call .close() don't we know that the file
is "finished"?

But when .close() fails?
Maybe, then you have an open but "finished" file. Could someone explain what
this "finished" term means. Is that an officially recognized state a file
can be in?
Best

Kai-Uwe Bux
Nov 14 '06 #10

P: n/a
Roland Pibinger wrote:
On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
"Roland Pibinger" wrote in message
After close() check the stream state. Otherwise you don't know "when a
file is finsihed".
Please explain this. After you call .close() don't we know that the file is
"finished"?

But when .close() fails?
Can .close() fail? I think not.

Nov 15 '06 #11

P: n/a
Daniel T. wrote:
Roland Pibinger wrote:
>On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
>"Roland Pibinger" wrote in message
After close() check the stream state. Otherwise you don't know "when a
file is finsihed".

Please explain this. After you call .close() don't we know that the
file is "finished"?

But when .close() fails?

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.
Best

Kai-Uwe Bux
Nov 15 '06 #12

P: n/a
Kai-Uwe Bux <jk********@gmx.netwrote:
Daniel T. wrote:
>Roland Pibinger wrote:
>>On Tue, 14 Nov 2006 04:35:11 -0800, "Jim Langston" wrote:
"Roland Pibinger" wrote in message
>>>>After close() check the stream state. Otherwise you don't know
"when a file is finsihed".

Please explain this. After you call .close() don't we know that
the file is "finished"?

But when .close() fails?

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 is_open() will subsequently and invariantly return false if close()
fails. Also, if close() returns a null pointer, that doesn't necessarily
mean that it failed (after all, it returns NULL if is_open() was false
on entry.

Quite a pickle. close() can "fail" but there is no way to know if it
actually did fail unless one first queries "is_open()". Also, if it does
fail, we are left with no options because despite that failure
"is_open()" will still equal false, and thus subsequent calls to close()
will continue to return NULL.

So what is the robust way to close a file?

if ( file.is_open() ) {
if ( file.close() == 0 ) {
// what do we do here? [A]
}
}

I guess in [A] above, we could try to re-open the file, if that failed
then the file object becomes useless and we are done with it, if it
succeeds, we can try to close it again. Then we are stuck alternating
between opening the file and closing it again until such time as either
open fails, or close succeeds. A rather non-deterministic problem, the
program could get locked in a infinite loop.
if ( file.is_open() ) {
while ( file.close == 0 ) {
file.open();
if ( ! file.is_open() ) {
break;
}
}
}

Something like that? :-(

--
To send me email, put "sheltie" in the subject.
Nov 15 '06 #13

P: n/a
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
// what do we do here? [A]
}
}
What about:

file.close();
if (file.fail()) { // or file.bad()
// error
}

Better avoid iostreams for real work.

Best wishes,
Roland Pibinger
Nov 15 '06 #14

P: n/a
Roland Pibinger wrote:
Better avoid iostreams for real work.
Sorry, what?
Nov 15 '06 #15

P: n/a
In article <45*************@news.utanet.at>,
rp*****@yahoo.com (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.

So which is it?

--
To send me email, put "sheltie" in the subject.
Nov 15 '06 #16

P: n/a
Daniel T. wrote:
In article <45*************@news.utanet.at>,
rp*****@yahoo.com (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.

So which is it?
For the one from your code, void. Sorry for the confusion.
Best

Kai-Uwe Bux
Nov 15 '06 #17

P: n/a
Kai-Uwe Bux <jk********@gmx.netwrote:
Daniel T. wrote:
rp*****@yahoo.com (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?

--
To send me email, put "sheltie" in the subject.
Nov 15 '06 #18

P: n/a
"Daniel T." <da******@earthlink.netwrote in message
news:da****************************@news.west.eart hlink.net...
Kai-Uwe Bux <jk********@gmx.netwrote:
>Daniel T. wrote:
rp*****@yahoo.com (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?
I would think the only reason close would fail is if the file wasn't open in
the first place. And if it wasn't opened in the first place, then it is
closed. Consider close may fail in ths case:

std::ifstream MyFile( "Test.txt" );
if ( MyFile.is_open() )
{
// blah blah
}
MyFile.close();

If it wasn't able to open MyFile (it didn't exist, whatever) I presume that
the .close() would fail, but realisitcally, who cares? I already did a test
to see if it was open and if I needed something specific to do if it wasn't
found then I would have had an else. In practice it probably makes more
sense to put the .close() inside the if block, but I tend to put it outside
the block just like that, so I know when I'm done with a file without
looking inside blocks.

What does the standard say about the close() method itself, under what
conditions is it allowed to fail?
Nov 15 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.