NOTE:
These are a little sketchy...I'll fill in details 'soon'.
Input Files
- ios::nocreate is no longer part of the standard
- sane behavior is supposed to be present which won't create
empty files when a requested input file doesn't exist
- this is only, of course, for standards compliant libraries
(your mileage may vary)
Output Files
- ios::noreplace is no longer part of the standard
- overwriting existing files is still the default behavior, however
- to avoid this, you must open the file for input to check its
existence and then (closing the input connection) open as
either ios_base::out (default) or ios_base::app (for appending)
— typically as the user wishes
Either Input or Output
- don't forget to close the connection even when it failed to
open properly — freeing up system resources
- you also, on very strict compilers, clear all the error flags
(after the close) to make sure all is set up properly (some
systems interpret that section of the standard VERY literally
and feel the flags aren't supposed to be set up; *shrug*)
Results for You
Input File:
prompt and read name
open (no mode required -- if your library is compliant)
while (!file)
{
close the file
clear the flags
print error message
prompt and read name
open (no mode required -- if your library is compliant)
}
Output File:
do
{
open_tried <-- false
prompt and read name
open for input (compliance determines if you need a mode flag)
if (!infile)
{
no problems -- open for output (no mode flags needed at all)
open_tried <-- true
}
else
{
ask user for overwrite or append (or choose different name?)
if (overwrite)
{
open for output (no mode flags needed at all)
open_tried <-- true
}
else if (append)
{
open for append (no other mode flags needed)
open_tried <-- true
}
if ((!new_name) && (!file))
{
close file
clear file
}
}
close infile connection
clear infile's flags
} while (!open_tried || !file);