0% found this document useful (0 votes)
16 views16 pages

File Handling

The document provides an overview of file handling in C++ using the fstream library, detailing the use of ifstream, ofstream, and fstream data types for file input and output. It explains how to read from and write to files using operators and member functions, including getline and character I/O methods. Additionally, it discusses file access flags and the ability to work with multiple files simultaneously.

Uploaded by

nehalkashif112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views16 pages

File Handling

The document provides an overview of file handling in C++ using the fstream library, detailing the use of ifstream, ofstream, and fstream data types for file input and output. It explains how to read from and write to files using operators and member functions, including getline and character I/O methods. Additionally, it discusses file access flags and the ability to work with multiple files simultaneously.

Uploaded by

nehalkashif112
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Using Files

1. Requires fstream header file


– use ifstream data type for input files
– use ofstream data type for output files
– use fstream data type for both input, output
files
2. Can use >>, << to read from, write to a file
3. Can use eof member function to test for
end of input file

Copyright © 2012 Pearson Education, Inc.


fstream Object

• fstream object can be used for either input or output


• Must specify mode on the open statement
• Sample modes:
ios::in – input
ios::out – output
• Can be combined on open call:
dFile.open("class.txt", ios::in | ios::out);

Copyright © 2012 Pearson Education, Inc.


File Access Flags

Copyright © 2012 Pearson Education, Inc.


Using Files - Example
// copy 10 numbers between files
// open the files
fstream infile("input.txt", ios::in);
fstream outfile("output.txt", ios::out);
int num;
for (int i = 1; i <= 10; i++)
{
infile >> num; // use the files
outfile << num;
}
infile.close(); // close the files
outfile.close();
Copyright © 2012 Pearson Education, Inc.
File Output Formatting

• Use the same techniques with file stream


objects as with cout: showpoint,
setw(x), showprecision(x), etc.

• Requires iomanip to use manipulators

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
Program 12-3 (Continued)

Copyright © 2012 Pearson Education, Inc.


Member Functions for Reading
and Writing Files
• Functions that may be used for input with
whitespace, to perform single character
I/O, or to return to the beginning of an input
file
• Member functions:
getline: reads input including whitespace
get: reads a single character
put: writes a single character

Copyright © 2012 Pearson Education, Inc.


The getline Function
• Three arguments:
– Name of a file stream object
– Name of a string object
– Delimiter character of your choice
– Examples, using the file stream object myFile,
and the string objects name and address:
getline(myFile, name);
getline(myFile, address, '\t');

– If left out, '\n' is default for third argument

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Single Character I/O
• get: read a single character from a file
char letterGrade;
gradeFile.get(letterGrade);
Will read any character, including whitespace

• put: write a single character to a file


reportFile.put(letterGrade);

Copyright © 2012 Pearson Education, Inc.


Working with Multiple Files
• Can have more than file open at a time in
a program

• Files may be open for input or output

• Need to define file stream object for each


file

Copyright © 2012 Pearson Education, Inc.


Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.

You might also like