The
classes overload the! operator. If an error has occurred, the! operator evaluates as true. We take advantage of this feature to check whether the file was opened successfully after its construction. The while loop also uses this operator implicitly to check the file's status on each iteration. Finally, when the program terminates, dictionary's destructor closes the file automatically.File modesIf you do not specify explicit open modes, fstream classes use default modes. For instance, ifstream opens a file in read mode by default and sets the .. file pointer at the file's beginning Similarly, ofstream opens a file in read mode You may combine multiple flags by using the bitwise OR operator like this: ofstream logfile ( "login.dat", ios :: binary | ios :: app) .; Listing B contains a list of the open modes and file attributes
File repositioningA file object has a logical pointer that points to a certain offset within the physical file. You can position this pointer to an arbitrary location by calling the seekp () member function. This member function advances the file position by a specified offset from the Current Position. in The Following Example, The Program Advances The File's Position 10 BYTES AND THEN CALLS TELLP () To Report The New Position: OFStream Fout ("Parts.txt"); fout.seekp (10); //move 10 bytes ahead from beginningcout << "new position:" << fout.tellp (); // display 10The seekp () member function has another overloaded version, which takes a direction as a second argument For instance, this function call moves the current. File Position Two Bytes Back from The Current Position. Fout.seekp (-3, iOS :: Cur); You Can Use the Following Constants To Indicate The Direction of The File's Position: ios :: Beg // Position At File's Beginningios :: Cur // Current Position, for Example: iOS :: Cur 5ios :: End // Position A t file's endCombine read and write operationsThe
library is wchar_t support. Each of the classes I've discussed thus far has a wchar_t equivalent that supports files containing wchar_t data. The names of these classes are wifstream, wofstream, and wfstream. However, the C standard supports only char-oriented filenames , so these classes may take only char * arguments, regardless of their content. Listing D shows a whcar_t version of the program we saw in Listing A. Notice again how simple the migration from char-oriented I / O to wchar_t is.
#include
#include
#include
Using namespace std;
int main ()
{String S; ifstream Dictionary ("Dict.txt"); if (! Dictionary) // WERE THERE ANY ERRORS ON OPENING? EXIT (-1); while (Dictionary >> s) cout << S << '/ n "
}
Listing B iOS :: App // Append
ios :: ate // Open and seek to file's end
ios :: binary // binary mode I / o (as opposed to text mode)
ios :: in // Open for Read
ios :: OUT // Open for WRITE
ios :: trunc // truncate file to 0 longthlisting c #include
#include
#include
int main ()
{TIME_T login; std :: string user; fstream logfile ("log.dat"); // read write logfile << Time (0) << "USER205" << '/ n'; // Write a new record Logfile.seek (ios :: beg); // rebind logfile >> login >> user; // read previously Written VALUES
}
Listing D
#include
#include
#include
#include
Using namespace std;
int main ()
{Wstring s; // wchar_t strings Wifstream Dictionary ("Chinese_dict.utf"); // Unicode File if (! Dictionary) // WERE The Any Errors ON OPENING? EXIT (-1); // Wcout Supports Wide Characters While Dictionary >> s) wcout << s << '/ n';}