c++ - 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.
推荐阅读
- react-native - Mapbox 支持 Flutter 和 React Native 吗?应用程序
- javascript - 获取下一个表格单元格onclick javascript的值
- reactjs - 用函数反应函数组件
- model-view-controller - 无法将 lambda 表达式转换为 ienumerable 类型,因为它不是 kendo grid mvc 中的委托类型
- wpf - 无论是绑定 Byte[] 还是字符串以获得良好的性能,哪种绑定对 Image 更好
- javascript - React native:从 1 个函数中获取数据并将其传递给 2 号函数的方法是什么?
- javascript - 如何将全局样式表导入组件 vue?
- appium - 如何修复 Appium 和 Python 的失败“INSTALL PARSE FAILED NO CERTIFICATES”?
- neo4j - 我计算传入关系的密码查询似乎很慢,需要一些帮助来提高性能
- python - 在 Python 中将数据从 MySQL 导出为 CSV