File.Close() is omitted?

I

Immortal Nephi

If I forget to put File.close() or File.close() is omitted in the
main function body, then do File’s destructor function takes care to
call File.close() automatically prior termination?

int main()
{
std::fstream File;

File.open( L"c:\\FileTest.txt", std::ios::eek:ut );

if( !File )
{
std::cerr << "Can't open file" << std::endl;
system( "pause" );
exit( true );
}

File << "Hello World!" << endl;

// File.close(); // is omitted?

return 0;
}
 
Ö

Öö Tiib

        If I forget to put File.close() or File.close() is omitted in the
main function body, then do File’s destructor function takes care to
call File.close() automatically prior termination?

yes. But if there were problems during flushing write buffer in close
you do not get to know of it.
 
I

Immortal Nephi

yes. But if there were problems during flushing write buffer in close
you do not get to know of it.

Please explain how flushing write buffer may have problems. I
consider to use File write buffer in the assert macro like creating
logs. I may be required to use File.open(...) and File.close() in the
same file to append buffer every time I invoke assert macro.
 
T

tonydee

Please explain how flushing write buffer may have problems.  I
consider to use File write buffer in the assert macro like creating
logs.  I may be required to use File.open(...) and File.close() in the
same file to append buffer every time I invoke assert macro.

The Operating System may be unable to complete a write for many
reasons, such as "disk full", quota exceeded, some lock conflict,
network error with a network-mounted filesystem, etc.. It is good
practice to check the state of your file, and offer some resolution
options or behaviours appropriate to the importance of the data that
might be lost. "assert" - if you mean the Standard-defined assert()
function - is not an appropriate check for the successful completion
of a write, as it terminates the program without attempting to work
around the issue, and is often disabled in production.

Cheers,
Tony
 
B

Bo Persson

Immortal said:
Please explain how flushing write buffer may have problems. I
consider to use File write buffer in the assert macro like creating
logs. I may be required to use File.open(...) and File.close() in
the same file to append buffer every time I invoke assert macro.

The File.close() operation might fail for some external reason
(perhaps OS related). If you call close() yourself, you can check the
return value and File state afterwards. If you let the destructor do
the close(), the File is gone so you don't see the result.


Bo Persson
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top