1.8 简单的文件输入输出
1. 将数据写入文本文件中, 包含几个要点:
(1) 必须包含头文件fstream
(2) 头文件fstream中定义了一个用于处理输出的ofstream类
(3) 需要声明一个或多个ofstream对象
(4) 需要将ofstream对象与文件关联起来, 方法之一是使用open()
(5) 使用完文件后, 应使用close()方法将其关闭
(6) 可结合使用ofstream对象和操作符<<来输出各种类型的数据.
定义一个ofstream类, ofstream outfile1; 然后将其与文件关联起来,使用对象的open方法来关联文件.例如下面几种:
(1) 直接以文件路径作为open参数:outfile1.open("fish.txt"). 如果没有fish.txt文件, open()方法将会创建一个fish.txt文件, 如果已经包含了一个fish.txt文件, open()将首先丢弃其原先的内容, 然后将新的输入加入到文件中.(当然有方法让我们保留原来的内容)
(2) open()以字符串作为参数:
char filename[50];
cin>>filename;
outfile1.open(filename);
创建了ofstream对象, 并与文件关联之后, 下面是怎么使用此对象了. 使用方法与cout对象类似, 结合<<操作符. 例如:
double w=125.8;
outfile1<<w; //将w写入对象outfile1所关联的文件fish.txt中
char line[81]="I have a dream!";
outfile1<<line<<endl; //将line中内容和一个换行符输入对象outfile1所关联的文件fish.txt中
总结文件输出的主要步骤:
(1) 包含头文件fstream; #include<fstream>
(2) 创建一个ofstream对象; ofstream outfile1;
(3) 将ofstream对象同一个文件关联起来; outfile1.open("fish.txt");
(4) 想使用cout那样使用ofstream对象; outfile1<<line<<endl;
#include<iostream>
#include<fstream>
using namespace std;int main()
{char automobile[50];int year;double a_price;ofstream outfile;outfile.open("fish.txt");cout<<"Enter your automobile's information: ";cin>>automobile;outfile<<"Enter your automobile's information: "<<automobile<<endl;cout<<"When your buy it: ";cin>>year;outfile<<"When your buy it: "<<year<<endl;cout<<"What's the price: ";cin>>a_price;outfile<<"What's the price: "<<a_price<<endl;outfile.close();/分割线,下面是数据//18 19 18.5 13.5 14//16 19.5 20 18 12 18.5char filename[60];ifstream infile;cin.get();//吸收一个换行符cout<<endl<<"Enter name of data file: ";cin.getline(filename,60);infile.open(filename);if(!infile.is_open()){cout<<"Fail to open the file!"<<endl;}double value;double sum=0;int count=0;infile>>value; //读取第一个一个double数据赋值给valuewhile(infile.good())//如果输入正确并且没有到文件结尾处(EOF),循环结束则可能有多个原因:1.到达文件尾部EOF,2.输入不正确,数据类型不匹配,3其他原因{++count;sum+=value;infile>>value;//继续读取数据}if(infile.eof())//如果到达文件尾部而停止while,则cout<<"Reach the end of file."<<endl;else if(infile.fail())//如果是因为数据不匹配,则cout<<"Input terminated by data mismatch./n";else//可能因为未知原因cout<<"Input terminated for unknown reason./n";infile.close();ofstream outfile2;outfile2.open("output.txt");if(count==0)//没有数据被处理cout<<"No data processed./n";else//有数据被处理则输出处理结果{cout<<"Items read: "<<count<<endl;cout<<"Sum: "<<sum<<endl;cout<<"Average: "<<sum/count<<endl;outfile2<<"Items read: "<<count<<endl;//输出结果到文件output.txtoutfile2<<"Sum: "<<sum<<endl;outfile2<<"Average: "<<sum/count<<endl;}outfile2.close();//输出://Items read: 10//Sum: 168.5//Average: 16.85system("pause");return 0;
}
2. 读入文本数据, 包含几个要点
(1) 必须包含头文件fstream
(2) 头文件fstream定义了一个用于处理输入的ifstream类
(3) 需要声明一个ifstream对象
(4) 将ifstream对象与文件关联
(5) 结合操作符>>来读取各种类型数据
(6) 使用ifstream对象的get()方法来读取一个字符, getline()方法读取一行字符
(7) 如果最后一个读取操作成功, 返回true, 否则返回false.
定义个ifstream对象: ifstream infile; 然后将这个对象与文件关联起来, 使用open()方法来关联文件. infile.open("fish.txt"); 这样使用对象:
double wt;
infile>>wt; //从file中读取一个数据
char line[81];
infile.getline(line, 81);//读取一行字符存入line中
如果打开的文件不存在, 则无法对对象进行输入, 用is_open()方法判断, 成功打开文件, 则infile.is_open()返回true, 否则返回false.
if(!infile.is_open())
cout<<"fail to open the file/n";
示例看上面的程序
总结
以上是生活随笔为你收集整理的1.8 简单的文件输入输出的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 1.7 时间延时器和类的别名
- 下一篇: OpenGL配置