06 Advanced File Operations Update
06 Advanced File Operations Update
06 Advanced File Operations Update
Programming Technique II
(SCSJ1023)
Adapted from Tony Gaddis and Barret Krupnow (2016), Starting out with
C++: From Control Structures through Objects
Content
File Operations
Binary Files
Random-Access Files
FILE OPERATIONS
File Operations
Can use eof member function to test for the end of input file.
fstream Object
The fstream object can be used for either input or output
Sample modes:
ios::in – for input files
ios::out – for output files
fstream infile("input.txt",ios::in);
fstream outfile("output.txt",ios::out);
int num;
ofstream:
open for output only
file cannot be read from
file created if no file exists
file contents erased if file exists
Function call
Program 12-5 (Continued)
Function call
Function definition
Program 12-5 (Continued)
Function definition
Member Functions for Reading
and Writing Files
Member Functions for Reading and
Writing Files
Member functions:
getline: reads input including whitespace
get: reads a single character
put: writes a single character
getline Method
For working with C-strings. Three arguments needed:
A c-string (i.e., character array) to hold input
Example:
ifstream myFile(“input.txt”);
char name[41], addr[41];
myFile.getline(name, 40); //using default delimiter ‘\n'
myFile.getline(addr, 40, '\t');
getline Member Function
getline function
For working with C++ strings
Prototype:
getline(fileObject, stringBuffer, delimiter);
Examples:
ifstream myFile(“input.txt”);
string name,addr;
getline(myFile, name); // using default delimiter ‘\n'
getline(myFile, addr, '\t');
getline Function
Input file
Screen output
Example of using different delimiters
Input file
Screen output
Single Character I/O
get: to read a single character from an input file.
Example:
ifstream gradeFile(“grade.txt”);
char letterGrade;
gradeFile.get(letterGrade);
Will read any character, including whitespace
inFile.open("nums.dat", ios::in|ios::binary);
Binary Files
Use read instead of <<, to read data from files.
Example: To read a letter from an input file
fstream fin(“input.dat“,ios::in|ios::binary);
char ch;
fin.read(&ch, sizeof(ch));
C-style:
fout.write((char *)&num, sizeof(num));
Example Application of Binary Files
Copying a file
Example Application of Binary Files
Writing and reading an object into/from a file
Example Application of Binary Files
Writing an array of objects into a file (1)
Example Application of Binary Files
Writing an array of objects into a file (2)
Example Application of Binary Files
Reading an array of objects from a file (1)
Screen output
Example Application of Binary Files
Reading an array of objects from a file (2)
Screen output
RANDOM-ACCESS FILES
Random-Access Files
Examples:
inData.seekg(25L, ios::beg);
// set read position at 26th byte
// from beginning of file
outData.seekp(-10L, ios::cur);
// set write position 10 bytes
// before current position
Important Note on Random Access
Example:
gradeFile.clear();
gradeFile.seekg(0L, ios::beg);
// go to the beginning of the file
Random Access Information
tellg member function: return current byte position in
input file
Example:
long int whereAmI;
whereAmI = inData.tellg();
whereAmI = outData.tellp();
Example Application of Random-Access
Determining the size of a file