Chapter_12_Review

advertisement

Chapter 12 – Advanced File Operations

Using a file in a program is a 3 step process:

1. open the file

2. process the file (read/write)

3. close the file

• use of file stream objects requires the inclusion of the fstream header file

• if fstream is declared, it is unnecessary to include iostream for use of cin / cout file stream classes:

• ifstream

• ofstream

• fstream file used for input (read only) file used for output (write only) file used for input, output or both declaration of a file stream object ifstream InputFile; ofstream OutputFile; fstream DataFile; use of the open( ) member function

InputFile.open(“customer.dat”);

DataFile.open(“info.dat”, ios::in | ios::out ); use of a backslash in a path for a file specification

OutputFile.open(“a:\\files\\invtry.dat”); // requires two backslashes

File Access Flags: ios::app, ios::ate, ios:: binary, ios::in, ios::out, ios::trunc

Default input mode for a file is as an ASCII file. Use the file access flag ios::binary to open a file and process it as a binary file. testing for open errors

• if ( !

DataFile) or if ( DataFile = = 0 )

• also: if ( DataFile.fail( ) ) closing a file

DataFile.close( );

Working with binary files

The number 123456 would require 6 characters ( bytes ) to store in an ASCII text file

To store the integer 123456 in binary in a binary file would require 4 bytes ( the size of an int ) writing to an ASCII file using the stream insertion operator << outputFile << “I love C++ programming…”; reading from an ASCII file using the stream extraction operator >> inputFile >> studentNumber; file output formatting

• all of the familiar stream manipulators and stream object member functions can be used with file stream objects

• setw, setprecision, fixed, etc. using the stream extraction operator to read information from a file >>

InputFile >> Emp_No;

InputFile >> FirstName; remember the extraction operator stops reading when it encounters any whitespace character ( space, tab, newline )

passing file stream objects to functions

 File stream objects may be passed by reference to functions bool openFileIn ( ifstream & file, char name[51] )

{

file.open(name);

if ( file.fail( ) )

status = false;

else

status = true;

return status;

} detecting the end of a file: if ( DataFile.eof( ) ) while ( !

DataFile.eof( ) ) eof( ) member function: returns 0 if not at the end of the file returns non-zero (true) if at the end of the file member functions for reading and writing files

• getline DataFile.getline(description, 81); or getline(DataFile, description); if description is a string.

• getline default delimiter character is the newline character ‘\n’ get and put member functions to read/write a character at a time

For binary files, the member functions read and write are used to input or output data. They require a reinterpret_cast if the data is not type char.

BinaryFile.read(reinterpret_cast<char *>(&number), sizeof(number));

BinaryFile.write(reinterpret_cast<char*>(&number), sizeof(number)); using a constructor in the declaration of a file stream object ifstream InputFile(“datafile.txt”); this is equivalent to: ifstream InputFile;

InputFile.open(“datafile.txt”);

Download