Reference:
Book: Starting Out with C++ from Control Structures to Objects, by Tony Gaddis, ninth edition
Concept: When a program needs to save data for later use, it writes the data in a file. The data can then be read from the file at a later time.
1. Setting Up a Program for File Output/Input
hearder file:
#include <fstream>
The <fstream> header file defines the data types ofstream, iftream and fstream.
Before a C++ program can work with a file, it must define an object of one of these data types. The object will be "linked" with an actual file on the computer's disk, and the operations that may be performed on the file depend on which of these three data types you pick for the file stream object.
File Stream Data Type
Description
ofstream
Output file stream. You create an object of this data tyoe when you want to create a file and write data to it.
ifstream
Input file stream. You create an object of this data type when you want to open an existing file and read data from it.
fstream
File stream. Objects of this data type can be used to open files for reading, writing, or both.
Input Stream – data stream from which information can be read
Ex: cin to read from the keyboard
Ex: ifstream objects to read data from files
Output Stream – data stream to which information can be written
Ex: cout to write to the computer screen
Ex: ofstream objects to write data into files
2. Output (writing)
Open a file for output (writing):
// The first statement defines an `ofstream` object named outputFile.
ofstream outputFile;
// The second statement calls the object's `open` member function,
// passing the string "Employees.txt" as an argument.
// The open function creates the txt file
// and links it with the outputFile object.
outputFile.open("Employees.txt");
Open a file for output (writing) in one statement:
// Define an `ofstream` object named outputFile and `open` a file.
ofstream outputFile("Employees.txt");
Example:
#include <iostream>
#include <fstream> // for File Input/Output
using namespace std;
int main() {
int num = 100;
// ofstream outfile;
// outfile.open("my-first-file.txt"); // outfile is similar to cout
ofstream outfile("my-first-file.txt");
cout << "This program is writing to your file." << endl;
outfile << "Hello world!" << endl;
outfile << 123.45 << endl;
outfile << num << endl;
outfile.close();
return 0;
}
Note that the my-first-file.txt file will be created in the directory:
your-project/cmake-build-debug , if you use JetBrains Clion.
Hello world!
123.45
100
3. Input (reading)
Reading data from a file.
The >> operator reads not only user input form the cin object, but also data from a file.
Assuming inputFile is an ifstream object, the following statement shows the >> operator reading data from the file into the variable name:
Reference: Book: Starting Out with C++ from Control Structures to Objects, by Tony Gaddis, ninth edition
1. Setting Up a Program for File Output/Input
hearder file:
The
<fstream>
header file defines the data typesofstream
,iftream
andfstream
.Before a C++ program can work with a file, it must define an object of one of these data types. The object will be "linked" with an actual file on the computer's disk, and the operations that may be performed on the file depend on which of these three data types you pick for the file stream object.
cin
to read from the keyboardifstream
objects to read data from filescout
to write to the computer screenofstream
objects to write data into files2. Output (writing)
Open a file for output (writing):
Open a file for output (writing) in one statement:
Example:
Note that the
my-first-file.txt
file will be created in the directory:your-project/cmake-build-debug
, if you use JetBrains Clion.3. Input (reading)
Reading data from a file.
The
>>
operator reads not only user input form thecin
object, but also data from a file.Assuming
inputFile
is anifstream
object, the following statement shows the>>
operator reading data from the file into the variable name:Example:
Output: