Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++ Using Files for Data Storage: Output and Input #198

Open Qingquan-Li opened 2 years ago

Qingquan-Li commented 2 years ago

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.



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:

inputFile >> name;

Example:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main() {
    string first_word, second_word;
    double num1;
    int whole_num;
    // ofstream infile;
    // infile.open("my-first-file.txt");
    ifstream infile("my-first-file.txt");

    if (infile) {
        cout << "====== File can be opened. ======" << endl;
        infile >> first_word;  // infile is similar to cin
        infile >> second_word;
        infile >> num1;
        infile >> whole_num;
        cout << first_word << " " << second_word << endl;
        cout << num1 << endl;
        cout << whole_num << endl;
    }
    else {
        cout << "====== Error opening file. ======" << endl;
    }
    infile.close();

    return 0;
}

Output:

====== File can be opened. ======
Hello world!
123.45
100