1.写入到文本文件中
文件输出做出的准备:
- 必须包含头文件fstream
- 头文件fstream定义了一个用于处理输出的ofstream类
- 需要声明一个或多个ofstream变量,并以自己喜欢的方式对齐进行命名,条件是遵守常用的命名规则。
- 必须指明命名空间std;
- 需要将ofstream对象与文件关联起来。为此,方法之一是使用open()方法。
- 使用完文件后,应适用方法close()将其关闭。
- 可以结合使用ofstream提供了一个预先定义好的名为cout的ostream对象,但你必须声明自己的ofstream对象,为其命名,并将其同文件关联起来。下面将演示如何声明这种对象:
ofstream outFile;//outFile an ofstream object ofstream fout;//fout an ofstream object
下面演示了如何将这种对象与特定的文件关联起来:
outFile.open("fish.text");//outFile used to write to the fish.txt file char filename[50]; cin>>filename;//user specifies 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]="Obiect are closer than ther appear";
fout<<line<<endl;//write a line of txt
重要的是,声明一个ofstream对象并将其通文件关联起来后,便可以像使用cout那样使用它。所有课用于cout 的操作和方法都可以用于ofstream对象。
以下程序演示了这种方法。他要求用于输入信息,然后将信息显示到屏幕上,再将这些信息写入到文件中。(生成的文件保留在该项目的文件里(没有创建文件的话编译器会自动建立一个))(如果存在他将丢其原有的内容,重新写入)
#include<iostream>
#include<fstream>
int main(void)
{
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;
cout << fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout << "Make and model: " << automobile << endl;
cout << "Yeat: " << year << endl;
cout << "Was asking $" << a_price << endl;
cout << "Now asking $" << d_price << endl;
outFile << fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile << "Make and model: " << automobile << endl;
outFile << "Yeat: " << year << endl;
outFile << "Was asking $" << a_price << endl;
outFile << "Now asking $" << d_price << endl;
outFile.close();//close file
return 0;
}
2.读取文本文件
读取文件所要准备的步骤:
- 可以结合使用cin和运算符>>来读取各种类型的数据。
- 可以使用cin和get()方法来读取一个字符,使用cin()和getline()来读取一行字符。
- 可以结合使用cin和eof()、fail()方法来判断输入是否成功。
- 对象cin本身被用作测试条件时,如果最后一个读取操作成功,他将转换为布尔值true,否则布被转换为false。
- 必须包含头文件fstream
- 头文件fstream定义了一个用于处理输入的ifstream类
- 需要声明一个或多个ifstream变量,并以自己喜欢的方式对齐进行命名,条件是遵守常用的命名规则。
- 必须指明命名空间std;
- 需要将ifstream对象与文件关联起来。为此,方法之一是使用open()方法。
- 使用完文件后,应适用方法close()将其关闭。
- 可以结合使用ifstream和eof()、fail()等方法来判断输入是否成功。
- ifstream对象本身被用作测试条件时,如果最后一个读取操作成功,他将被转化为布尔值true,否则被转化为flase。
- 注意,虽然头文件iostream提供了一个预先定义好的名为cin的istream对象,但你必须声明自己的iftream对象,为其命名,并将其同文件名关联起来。
- 其同文件关联起来。下面将演示如何声明这种对象:
ifstream inFile; ifstream fin;
下面演示了如何将这种对象与特定文件关联起来:
inFile.open("bowling.txt");//inFile used to read bowling.txt file char filename[50]; cin>>filename;//user specifies a name fin.open(filename);//fin used to read specified
注意,方法open()接收一个C-风格字符串作为参数,这可以是一个字面字符串,也可以是存储在数组中的字符串。下面演示了如何使用这种对象。
double wt; inFile>>wt;//read a number from bowling.txt char line[81]; fin.getline(line,81);//read a line of text
重要的是,声明一个iftream对象并将其同文件关联起来后,便可以项实用cin那样使用它。所有可用于cin操作和方法都可于iftream对象。
如果试图打开一个不存在的文件用于输入,这种错误将导致后面使用iftream对象进行输入时失败。检查文件是否被成功打开的首先方法是使用方法is_open(),为此,可以使用类似于下面的代码:
inFile.open("bowling.txt");
if(!inFile.is_open())
{
exit(EXIT_FAILURE);
}
文件成功打开,方法is_open()将返回true;函数exit()的原型是在头文件cstdlib中定义的,在该头文件中,还定义了一个同操作系统通信的参数值EXIT_FAILURE。函数exit()终止程序。
#include<iostream>
#include<fstream>
#include<cstdlib>
const int SIZE = 60;
int main(void)
{
using namespace std;
char filename[SIZE];
ifstream inFile;
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file" << filename << endl;
cout << "Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
inFile >> value;
while (inFile.good())//可以替换成is_open();
{
++count;
sum += value;
inFile >> value;
}
if (inFile.eof())
cout << "End of file reach.\n";
else if (inFile.fail())
cout << "Input terminated by data mismatch.\n";
else
cout << "No data processed.\n";
if (count==0)
cout << "No data processed.\n";
else
{
cout << "Items read: " << count << endl;
cout << "Sum: " << sum << endl;
cout << "Average: " << sum / count << endl;
}
inFile.close();
return 0;
}
- 如果最后一次读取数据时遇到EOF,方法eof()将返回true。
-
如果最后一次读取操作中发生了类型不匹配的情况fail()将返回true。
-
如果最后一次读取文件是发生文件受损或硬件故障则bad()将返回true。