File Input and Output
Chapter 3
Introduction
A file is an area in secondary storage to hold info.
We use streams that are similar to cin
and count with files, except they are fstream
variables that we define.
File I/O is a five-step process:
- Include the
fstream
header. - Declare file stream variables (of time
ifstream
for input andofstream
for output). - Associate the file stream variables with the input/output sources.
- Use the file stream variables with
>>
,<<
, or other input/output functions. - Close the file stream (don’t forget this step).
Creating Files
Reading from Files
Example: Reading and Writing
cpp
#include <fstream> // the preprocessor directive for using files
using namespace std;
int main() {
// Declare variables
...
// Declare that you will use a file stream.
ifstream inData; // input file stream
ofstream outData; // output file stream
// Read in values
inData.open("text.txt"); // opens data for reading
inData >> firstName >> lastName >> GPA; // gets the data out of text.txt
inData.close();
...
// Write values to new file
outData.open("out.txt"); // creates a new file for output
outData << finalGrade; // put the data into out.txt
outData.close();
return 0;
}
Common Errors
- Forgot to open your file. It will still compile and run, but give you erroneous results.
- Tried to read input from a file that does not exist. Make sure the file is in the correct directly and that your code didn’t misspell the name.
- If you specify a directory (folder) location in conjunction with your filename (e.g.,
"C:\\my stuff\\data.txt"
), use forward slashes or escape your backslashes (e.g.,"c:/my stuff/data.txt"
or"c:\\my stuff\\data.txt"
). - Avoid using absolute paths, which are paths that refer to the file location from the root of the drive rather than a path relative to the program’s location.
Self-Check Questions
- Name the five steps for reading from a file.
- Name the five steps for writing to a file.
- If you are opening a file and it is not there, what happens?
- If you are writing to a file that doesn’t exist yet, what happens?