简体   繁体   中英

What do cin and cout do when an ofstream file is opened?

When an ofstream file outfile is opened, is only the data read by outfile << written into the file or are the messages displayed by cout also stored in the file?
And is the information taken by cin merely a temporary storage or does the cin write into the file as well? For instance I have taken a program from Files stream Example .

    #include <fstream>
    #include <iostream>
    using namespace std;

    int main () {
    char data[100];

    // open a file in write mode.
    ofstream outfile;
    outfile.open("afile.dat");

    cout << "Writing to the file" << endl;
    cout << "Enter your name: "; 
    cin.getline(data, 100);

    // write inputted data into the file.
    outfile << data << endl;

    cout << "Enter your age: "; 
    cin >> data;
    cin.ignore();

    // again write inputted data into the file.
    outfile << data << endl;

    // close the opened file.
    outfile.close();

    // open a file in read mode.
    ifstream infile; 
    infile.open("afile.dat"); 

    cout << "Reading from the file" << endl; 
    infile >> data; 

    // write the data at the screen.
    cout << data << endl;

    // again read the data from the file and display it.
    infile >> data; 
    cout << data << endl; 

    // close the opened file.
    infile.close();

    return 0;
    }

Here is cout << Writing to the file written in the file, for example? And also, is cin only used for getting the values and as a temporary storage space? Or is each cin written in the file directly?

If you scroll down a bit further on https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm , you will see the a sample run of the program.

$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9

If you compile and run the program yourself, inside the afile.dat file that gets created, you will see the following:

Zara
9

From that, we can figure out that only the data inserted into the outfile output file stream is written to the file, and that the messages sent to cout are not stored in the file (they only get shown to the user).

cout and cin are special -- they are streams for standard output and standard input, which by default are attached to the user's terminal window. They give you a way to write information to the terminal window that will be seen by a user ( cout ), and a way to read what a user types in ( cin ). There is also cerr , which is used for outputting errors to a user.

You can think of streams sort of like faucets and drains. Data gets read from an input stream (water coming from a faucet), and data gets written to an output stream (water going into a drain); your code can manipulate the input data and change it or move it place, sort of like a hose or pipe controlling where water flows to. Standard input/output/error are one example of representing interaction with the user's terminal as "streams", but you can use a similar method of inputting and outputting data as "streams" of data with files.

From the example output, you can see that the user typed in Zara and 9 , which got read by using the cin stream when the program prompted the user to enter a name and age by writing to the cout stream. The program then took the data read from the standard input and used a output file stream ( ofstream ) to write the data to a file, and then later the program reads the data from the file using an input file stream ( ifstream ) and shows it to the user by writing to standard out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM