You are on page 1of 14

CPSC 122 – Computer Science II

Gonzaga University – Department of Computer Science


File Streams
This statement opens the file info.txt in output modes. Output mode allows
data to be written to a file

This statement opens the file info.txt in input modes. Input mode allows data
to be read from a file

This statement opens the file info.txt in both input and output modes.
File Access (fstream)
Example
Example
š Assuming that diskInfo is an fstream object, write a statement that opens the file
names.dat for output.

š Write a statement that defines an fstream object named dataFile and opens a file named
salesfigures.txt for input. (Note: The file should be opened with the definition statement,
not an open function call.)
Functions and File Parameters

File stream objects may be passed to functions, but they should always be passed by
reference.

bool openFileIn(fstream &file, string name) ;


Example
Example
Checkpoint
Jayne Murphy$47 Jones Circle$Almond, NC
28702\n$Bobbie Smith$ 217 Halifax Drive$Canton, NC
28716\n$Bill Hammet$PO Box 121$ Springfield, NC
28357\n$
getline

getline(dataFile, input, '$');


while (dataFile)
{
// Display the last item read.
cout << input << endl;
// Read an item using $ as a delimiter.
getline(dataFile, input, '$');
}

You might also like