- 熟练掌握用程序直接读取文件中的数据而不是手工输入。
Master how to use program to read the data in the file directly instead of inputting manually. - 学会让程序将输出写入到文件从而得到有关结果的永久性记录。
Learn to have the program write output to a file to get a permanent record of the results.
[Experiment topic 1]
编写程序,创建文件mul.txt,其内容为九九乘法表,
要求通过循环自动生成该表,其中每个数据占4个字符位置,右对齐。
Experimental procedure
#include<iostream>
#include<fstream>
using namespace std;
int main() {
//用二维数组来构造乘法表
int mul[10][10] = { 0 };
int i(0), j(0);
//先初始化乘法表的因子
for (j = 1; j <= 9; j++)mul[0][j] = j;
for (i = 1; i <= 9; i++)mul[i][0] = i;
for (i = 1; i <= 9; i++) {
for (j = 1; j <= 9; j++) {
mul[i][j] = mul[i][0] * mul[0][j];
}
}//九九乘法表在二维数组中储存完毕
ofstream mul_out; //定义输出流对象
mul_out.open("mul.txt");//mul_out used to write to the mul.txt file
//先输出第一行
mul_out << " ";//数组第一个元素不输出进文件,满足格式则需空5格
for (j = 1; j <= 9; j++) {
mul_out.width(4);
mul_out << mul[0][j];
}
mul_out << endl;
//在输出剩下的
for (i = 1; i <= 9; i++) {
for (j = 0; j <= 9; j++) {
mul_out.width(4);
mul_out<< mul[i][j];
}mul_out << endl;
}
mul_out.close(); //done with file
//屏幕上也输出乘法表
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
cout.width(4);
cout << mul[i][j];
}cout << endl;
}
return 0;
}
Experimental results and analysis
屏幕输出是使用cout的结果。如果您查看该程序的可执行文件所在的目录,将看到一个名为mul.txt的文件(根据编译器的配置,该文件也可能位于其他文件夹),其中包含使用mul_out生成的输出。如果使用文本编译器打开该文件,将发现其内容如下:
Experimental process analysis
声明一个ofstream对象后,便可以使用方法open()将该对象特定文件关联起来:
After declaring an OFSTREAM object, you can use the method open () to associate the object specific files:
注意,方法close()不需要使用文件名作为参数,这是因为outFile已经同特定的文件关联起来。如果您忘记关闭文件,程序正常终止时将自动关闭它。mul_out可以使用cout可使用的任何方法。他不但能够使用运算符<<,还可以使用各种格式化方法,如setf()和precision()。这些方法只影响调用它们的对象。
Note that the method close() does not need to use the filename as an argument because the outfile is already associated with a specific file. If you forget to close the file, it will close automatically when the program terminates normally.
mul_ Out can use any method cout can use. He can not only use the operators < < but also use various formatting methods, such as setf() and precision(). These methods affect only the objects that call them.
注意,方法open()接受一个C-风格字符串作为参数,这可以是一个字面字符串,也可以是存储在数组中的字符串。
下面演示了如何使用这种对象:
double wt = 125.8;
outFile<<wt; //write a number to fish.txt
char line[81] = “Objects are closer than they appear.”;
fout<<line<<endl; //write a line of tex
输出对齐有两个方面,一是输出宽度,一是左对齐还是右对齐。在C++里面,默认是右对齐,可以通过cout.setf(std::ios::left)调整为左对齐,而且这种调整是全局的,一次设置,后面都有效。但是对于输出宽度的设置(使用cout.width(int i)设置)是一次性的,只影响紧随其后的一次输出。
There are two aspects of output alignment, one is output width, the other is left alignment or right alignment. In C + +, the default is right alignment. You can use the cout.setf (STD:: IOS:: left) adjust to left alignment, and this adjustment is global. It is set once and valid later. But for the output width setting (using cout.width (int i) setting) is one-time, affecting only the next output.
Empirical conclusioin
总之,使用文件输出的主要步骤如下:
In summary, the main steps to use file output are as follows:
1. 包含头文件fstream。
2. 创建一个ofstream对象。
3. 将该ofstream对象同一个文件关联起来。
4. 就像使用cout那样使用该ofstream对象。
- Include the header file fstream.
- Create an OFSTREAM object.
- Associate the OFSTREAM object with a file.
- Use the OFSTREAM object just as you would with cout.
2020.5.22
By Suki