22-写入到文本文件中
6.8.2 写入到文本文件中
对于文件输出,C++使用类似于cout的东西。下面来复习一些有关将cout用于控制台输出的基本事实,为文件输出做准备。
- 必须包含头文件iostream。
- 头文件iostream定义了一个用处理输出的ostream类。
- 头文件iostream声明了一个名为cout的ostream变量(对象)。
- 必须指明名称空间std;例如,为引用元素cout和endl,必须使用编译指令using或前缀std::。
- 可以结合使用cout和运算符<<来显示各种类型的数据。
文件输出与此极其相似。
- 必须包含头文件fstream。
- 头文件fstream定义了一个用于处理输出的ofstream类。
- 需要声明一个或多个ofstream变量(对象),并以自己喜欢的方式对其进行命名,条件是遵守常用的命名规则。
- 必须指明名称空间std;例如,为引用元素ofstream,必须使用编译指令using或前缀std::。
- 需要将ofstream对象与文件关联起来。为此,方法之一是使用open()方法。
- 使用完文件后,应使用方法close()将其关闭。
- 可结合使用ofstream对象和运算符<<来输出各种类型的数据。
注意,虽然头文件iostream提供了一个预先定义好的名为cout的ostream对象,但您必须声明自己的ofstream对象,为其命名,并将其同文件关联起来。下面演示了如何声明这种对象:
ofstream outFile; // outFile an ofstream object
ofstream fout; // fout an ofstream object
下面演示了如何将这种对象与特定的文件关联起来:
outFile.open("fish.txt"); // outFile used to write to the fish.txt file
char filename[50];
cin >> filename; // user specifies a name
fout.open(filename); // fout used to read specified file
注意,方法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 text
重要的是,声明一个ofstream对象并将其同文件关联起来后,便可以像使用cout那样使用它。所有可用于cout的操作和方法(如<<、endl和setf())都可用于ofstream对象(如前述示例中的outFile和fout)。
总之,使用文件输出的主要步骤如下。
1.包含头文件fstream。
2.创建一个ofstream对象。
3.将该ofstream对象同一个文件关联起来。
4.就像使用cout那样使用该ofstream对象。
程序清单6.15中的程序演示了这种方法。它要求用户输入信息,然后将信息显示到屏幕上,再将这些信息写入到文件中。读者可以使用文本编辑器来查看该输出文件的内容。
程序清单6.15 outfile.cpp
// outfile.cpp -- writing to a file
#include <iostream>
#include <fstream> // for file I/O
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile; // create object for output
outFile.open("carinfo.txt"); // associate with a file
cout << "Enter the make and model of automobile: ";
cin.getline(automobile, 50);
cout << "Enter the model year: ";
cin >> year;
cout << "Enter the original asking price: ";
cin >> a_price;
d_price = 0.913 * a_price;
// display information on screen with cout
cout << fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << "Make and model: " << automobile << endl;
cout << "Year: " << year << endl;
cout << "Was asking $" << a_price << endl;
cout << "Now asking $" << d_price << endl;
// now do exact same things using outFile instead of cout
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "Make and model: " << automobile << endl;
outFile << "Year: " << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << d_price << endl;
outFile.close(); // done with file
return 0;
}
该程序的最后一部分与cout部分相同,只是将cout替换为outFile而已。下面是该程序的运行情况:
Enter the make and model of automobile: Flitz Perky
Enter the model year: 2009
Enter the original asking price: 13500
Make and model: Flitz Perky
Year: 2009
Was asking $13500.00
Now asking $12325.50
屏幕输出是使用cout的结果。如果您查看该程序的可执行文件所在的目录,将看到一个名为carinfo.txt的新文件(根据编译器的配置,该文件也可能位于其他文件夹),其中包含使用outFile生成的输出。如果使用文本编辑器打开该文件,将发现其内容如下:
Make and model: Flitz Perky
Year: 2009
Was asking $13500.00
Now asking $12325.50
正如读者看到的,outFile将cout显示到屏幕上的内容写入到文件carinfo.txt中。
程序说明
在程序清单6.15的程序中,声明一个ofstream对象后,便可以使用方法open()将该对象特定文件关联起来:
ofstream outFile; // create object for output
outFile.open("carinfo.txt"); // associate with a file
程序使用完该文件后,应该将其关闭:
outFile.close();
注意,方法close()不需要使用文件名作为参数,这是因为outFile已经同特定的文件关联起来。如果您忘记关闭文件,程序正常终止时将自动关闭它。
outFile可使用cout可使用的任何方法。它不但能够使用运算符<<,还可以使用各种格式化方法,如setf()和precision()。这些方法只影响调用它们的对象。例如,对于不同的对象,可以提供不同的值:
cout.precision(2); // use a precision of 2 for the display
outFile.precision(4); // use a precision of 4 for file output
读者需要记住的重点是,创建好ofstream对象(如outFile)后,便可以像使用cout那样使用它。
回到open()方法:
outFile.open("carinfo.txt");
在这里,该程序运行之前,文件carinfo.txt并不存在。在这种情况下,方法open()将新建一个名为carinfo.txt的文件。如果在此运行该程序,文件carinfo.txt将存在,此时情况将如何呢?默认情况下,open()将首先截断该文件,即将其长度截短到零——丢弃原有的内容,然后将新的输出加入到该文件中。第17章将介绍如何修改这种默认行为。
警告: 打开已有的文件,以接受输出时,默认将它其长度截短为零,因此原来的内容将丢失。
打开文件用于接受输入时可能失败。例如,指定的文件可能已经存在,但禁止对其进行访问。因此细心的程序员将检查打开文件的操作是否成功,这将在下一个例子中介绍。