打开 ofstream 文件时 cin 和 cout 做什么?
What do cin and cout do when an ofstream file is opened?
当ofstream
文件outfile
打开时,是只有outfile <<
读取的数据写入文件还是cout
显示的消息也存储在文件?
cin
获取的信息只是临时存储还是 cin
也写入文件?例如,我从 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;
}
这里是cout <<
写入文件写入文件,例如?还有,cin
只是用来获取值和临时存储 space 吗?还是每个cin
直接写在文件里?
如果在 https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm 上再向下滚动一点,您将看到该程序的示例 运行。
$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9
如果您自己编译并 运行 程序,在创建的 afile.dat
文件中,您将看到以下内容:
Zara
9
从中我们可以看出,只有插入到 outfile
输出文件流中的数据才会写入文件,而发送到 cout
的消息不会存储在文件中(它们只显示给用户)。
cout
和 cin
是特殊的 -- 它们是标准输出和标准输入的流,默认情况下附加到用户的终端 window。它们为您提供了一种将信息写入终端 window 并将被用户看到 (cout
) 的方法,以及一种读取用户输入内容的方法 (cin
)。还有cerr
,用于向用户输出错误。
您可以将溪流想象成水龙头和下水道。数据从输入流(水从水龙头流出)中读取,数据写入输出流(水流入下水道);您的代码可以操纵输入数据并更改它或移动它的位置,有点像控制水流向的软管或管道。标准 input/output/error 是将与用户终端的交互表示为 "streams" 的一个示例,但您可以使用类似的方法输入和输出数据,如 "streams" 数据与文件。
从示例输出中,您可以看到用户键入 Zara
和 9
,当程序提示用户输入时使用 cin
流读取它们通过写入 cout
流来输入姓名和年龄。然后程序从标准输入中读取数据并使用输出文件流 (ofstream
) 将数据写入文件,然后程序使用输入文件流 (ifstream
) 并通过写入标准输出显示给用户。
当ofstream
文件outfile
打开时,是只有outfile <<
读取的数据写入文件还是cout
显示的消息也存储在文件?
cin
获取的信息只是临时存储还是 cin
也写入文件?例如,我从 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;
}
这里是cout <<
写入文件写入文件,例如?还有,cin
只是用来获取值和临时存储 space 吗?还是每个cin
直接写在文件里?
如果在 https://www.tutorialspoint.com/cplusplus/cpp_files_streams.htm 上再向下滚动一点,您将看到该程序的示例 运行。
$./a.out
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9
如果您自己编译并 运行 程序,在创建的 afile.dat
文件中,您将看到以下内容:
Zara
9
从中我们可以看出,只有插入到 outfile
输出文件流中的数据才会写入文件,而发送到 cout
的消息不会存储在文件中(它们只显示给用户)。
cout
和 cin
是特殊的 -- 它们是标准输出和标准输入的流,默认情况下附加到用户的终端 window。它们为您提供了一种将信息写入终端 window 并将被用户看到 (cout
) 的方法,以及一种读取用户输入内容的方法 (cin
)。还有cerr
,用于向用户输出错误。
您可以将溪流想象成水龙头和下水道。数据从输入流(水从水龙头流出)中读取,数据写入输出流(水流入下水道);您的代码可以操纵输入数据并更改它或移动它的位置,有点像控制水流向的软管或管道。标准 input/output/error 是将与用户终端的交互表示为 "streams" 的一个示例,但您可以使用类似的方法输入和输出数据,如 "streams" 数据与文件。
从示例输出中,您可以看到用户键入 Zara
和 9
,当程序提示用户输入时使用 cin
流读取它们通过写入 cout
流来输入姓名和年龄。然后程序从标准输入中读取数据并使用输出文件流 (ofstream
) 将数据写入文件,然后程序使用输入文件流 (ifstream
) 并通过写入标准输出显示给用户。