1、写入到文本文件
对于文件输入,c++使用类似于cout的东西。先复习一些有关将cout用于控制台输出的基本事实,为文件输出做准备。
控制台输出:
- 必须包含头文件iostream。
- 头文件iostream定义了一个用于处理输出的ostream类。
- 头文件 iostream声明了一个名为cout的ostream对象。
- 必须指明名称空间std;
- 结合使用cout和<<来显示各种类型的数据。
文件输出:
- 必须包含头文件fstream。
- 头文件fstream定义了一个用于处理输出的ofstream类。
- 需要声明一个或多个ofstream变量(对象),并以自己 喜欢的方式 对其命名,条件是遵守常用的命名规则。
- 必须指明名称空间std;
- 需要将ofstream变量(对象)与文件关联起来,为此,方法之一是使用open ()方法。
- 使用完文件之后,利用方法close()将其关闭。
- 结合使用ofstream和<<来显示各种类型的数据。
例程:
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
char name[50];
int year;
double math;
double english;
ofstream outFile;
outFile.open("1.txt");
/*控制台输入*/
cout<<"Enter your name:";
cin.getline(name,50);
cout<<"Enter the year:";
cin>>year;
cout<<"math score:";
cin>>math;
cout<<"english score:";
cin>>english;
/*显示在屏幕上*/
//控制输出格式
cout<<fixed;
cout.precision(2);
cout.setf(ios_base::showpoint);
cout<<"Make and model:"<<name<<endl;
cout<<"Year:"<<year<<endl;
cout<<"math:"<<math<<endl;
cout<<"english:"<<english<<endl;
/*写入文件*/
outFile<<fixed;
outFile.precision(2);
outFile.setf(ios_base::showpoint);
outFile<<"Make and model:"<<name<<endl;
outFile<<"Year:"<<year<<endl;
outFile<<"math:"<<math<<endl;
outFile<<"english:"<<english<<endl;
/*关闭文件*/
outFile.close();
return 0;
}
创建的TXT文件:
需要注意的是:创建好ofstream对象(outFile)后便可以像使用cout那样来使用outFile
关于open ()方法,程序运行之前,文件不存在,方法open()将会新建一个名为1.txt的文本文件。若文件存在,默认情况下,open()将会截断该文件,即将它的长度截断为0—丢掉原来的文件内容,将新的输出写入到文件。
2、读取文本文件
控制台输入:
- 必须包含头文件iostream。
- 头文件iostream定义了一个用于处理输出的istream类。
- 头文件 iostream声明了一个名为cin的istream对象。
- 必须指明名称空间std;
- 结合使用cin和>>来显示各种类型的数据。
- 可以使用cin和get()来读取一个字符,也可以使用cin和getline()来输入一行字符。
- 可以结合使用cin和eof()、fail()方法来判断输入是否成功。
文件输出:
- 必须包含头文件fstream。
- 头文件fstream定义了一个用于处理输入的ifstream类。
- 需要声明一个或多个ifstream变量(对象),并以自己 喜欢的方式 对其命名,条件是遵守常用的命名规则。
- 必须指明名称空间std;
- 需要将ifstream变量(对象)与文件关联起来,为此,方法之一是使用open ()方法。
- 使用完文件之后,利用方法close()将其关闭。
- 结合使用ifstream对象和>>来显示各种类型的数据。
- 可以使用ifstream对象和get()方法来读取一个字符,使用ifstream和getline()来读取一行字符。
- 可以结合使用ifstream对象和eof()、fail()方法来判断输入是否成功。
-
例程
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE=60;
int main()
{
using namespace std;
char fileName[SIZE];
cout<<"Enter name of data file:";
cin.getline(fileName,SIZE);
ifstream inFile;
inFile.open(fileName);
/*判断文件是否打开*/
if (!inFile.is_open())
{
cout<<"Could not open the file "<<fileName<<endl;
cout<<"Program terminating."<<endl;
exit(EXIT_FAILURE);
}
/*读取文件*/
double single;
double sum=0.0;
int count=0;
inFile>>single;
while (inFile.good())
{
++count;
sum+=single;
inFile>>single;
}
/*判断文件数据读取结束的原因*/
if (inFile.eof())
{
cout<<"End the data"<<endl;
}
else
if (inFile.fail())
{
cout<<"Input terminated by data mismath"<<endl;
}
else
{
cout<<"Input terminated by unknowed method"<<endl;
}
/*计算数字和以及均值*/
if (count==0)
{
cout<<"No data processed"<<endl;
}
else
{
cout<<"The number of data:"<<count<<endl;
cout<<"Sum:"<<sum<<endl;
cout<<"Average"<<sum/count<<endl;
}
/*关闭文件*/
inFile.close();
return 0;
}
输出结果:
txt文件:
值得注意的是:
- 检查文件是否打开成功使用方法is_open(),如果成功打开返回true,否则调用exit()函数,头文件在cstdlib中定义,还定义了一个用于同操作系统通信的参数值EXIT_FAILURE。函数exit()终止程序。exit(EXIT_FAILURE);表示程序非正常退出。exit(EXIT_SUCCESS);表示程序正常退出。
- 读取文件时,利用几个函数判断文件终止读取是基于什么原因比如eof()、fail()、bad()函数等,还有一种更简单的方式good(),该函数没有发生任何错误时,返回true。