Alternative to iS :: NOCREATE AND os :: NorePlace

xiaoxiao2021-03-06  104

Alternative to iS :: NOCREATE AND os :: NorePlace

In pre-standard C , certain implementations of offered the flags ios :: nocreate and ios :: noreplace for controlling file creation. These flags were too platform-specific and never made it into the standard library, which supersedes the deprecated, pre-standard header. However, you can achieve the functionality of these obsolete flags rather easily. The following example imitates the ios :: nocreate flag. First, it attempts to open a file for read IF The file is create, no new file is created. If the file exists, the program: iposs:

FSTREAM FS ("FNAME", iOS_BASE :: IN); // Attempt Open for Read

IF (! fs)

{

// file doesn't exist; don't create a new one

}

Else // OK, File EXISTS. Close and Reopen in Write Mode

{

fs.close ();

FS.Open ("FNAME", iOS_BASE :: OUT); // Reopen for Write

}

You can Just Do The Opposite for ios :: NorePlace:

FSTREAM FS ("FNAME", iOS_BASE :: IN); // Attempt Open for Read

IF (! fs)

{

// file doesn't exist; Create a new one

fs.open ("FName", iOS_Base :: OUT);

}

Else // OK, File Exists; Close and Reopen in Write Mode

{

fs.close ()

FS.Open ("FNAME", iOS_BASE :: OUT); // Reopen for Write

}

//however, it see the ms vc6.0 can not help import fstream Perfectly

转载请注明原文地址:https://www.9cbs.com/read-101863.html

New Post(0)