为了便于理解,我们把cout 用于控制台输出时的一些情况和写入到文本文件的情况进行类比:
cout 控制台输出
包含头文件 iostream
头文件 iostream 定义了一个 ostream 类用于处理输出
头文件 iostream 声明了一个名为 cout 的 ostream 对象
指明名称空间 std
可以结合使用cout和运算符<<来显示各种类型的数据
文本文件输出(写入到文本文件)
包含文件头 fstream
头文件 fstream 定义了一个ofstream 类用于处理输出
声明一个或多个 ofstream 对象,可以自由命名
指明名称空间 std
把 3 中声明的 ofstream 对象与文件关联起来,比如使用 open()方法
使用完文件后,需要使用 close()方法将其关闭
还可以结合 ofstream 对象和运算符<<来输出各种类型的数据
注意:cout 控制台输入输出中,头文件 iostream 声明了一个名为 cout 的 ostream 对象,无需自己手动声明;文件输出中,我们必须自己动手声明一个 ofstream 对象,为其命名,将其同文件关联起来。请看下面的例子:
1
2
3
|
#include<fstream> ofstream OutFile; //声明一个 ofstream 对象 OutFile.open( "study.txt" ); //将OF与“study.txt”文件关联起来 |
下面请看一个完整的程序,用户输入信息,将信息显示到屏幕上,再将这些信息写入到文本文件中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include <iostream> #include <fstream> using namespace std; int main() { char name[20]; double height; double weight; ofstream outFile; //创建了一个ofstream 对象 outFile.open( "information.txt" ); //outFile 与一个文本文件关联 cout<< "Enter name: " ; cin.getline(automobile, 20); cout<< "Enter height: " ; cin>>year; cout<< "Enter weight: " ; cin>>weight; // 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; // outFile 把信息写入到文本文件 outFile<<fixed; //小数点格式显示double outFile.precision(2); //设置精度 outFile.setf(ios_base::showpoint); //强制显示小数点后的零 outFile<< "Name: " <<name<<endl; outFile<< "Height: " <<height<<endl; outFile<< "Weight: " <<weight<<endl; outFile.close(); //使用完文本文件后要用close()方法将其关闭 return 0; } |
接下来,我们再把cin 用于控制台输入时的一些情况和读取文本文件的情况进行类比:
cin 控制台输出
包含头文件 iostream
头文件 iostream 定义了一个 istream 类用于处理输出
头文件 iostream 声明了一个名为 cin 的 istream 对象
指明名称空间 std
可以结合使用cin和运算符>>来输入各种类型的数据
文本文件输入(读取文本文件)
包含文件头 fstream
头文件 fstream 定义了一个ifstream 类用于处理输出
声明一个或多个 ifstream 对象,可以自由命名
指明名称空间 std
把 3 中声明的 ifstream 对象与文件关联起来,比如使用 open()方法
使用完文件后,需要使用 close()方法将其关闭
还可以结合 ifstream 对象和运算符>>来读取各种类型的数据
可以用 cin 和 get() 方法来读取一个字符,或用 cin 和 getline() 方法来读取一行字符
可以结合使用 ifstream 和 eof()、fail()方法来判断输入是否成功
如果试图打开一个不存在的文件用于输入将会导致后面使用 ifstream 对象读取数据时发生错误,因此用户需要使用 is_open() 方法检查文件是否被成功打开,如下:
1
2
3
4
5
6
7
|
#include <fstream> #include <cstdlib> ifstream InFile; InFile.open( "information.txt" ); if (!Infile.is_open()){ exit (EXIT_FAILURE); } |
如果文件被成功打开, is_open() 方法将返回 true , exit()的原型在头文件 cstdlib 中被定义,exit(EXIT_FAILURE) 用于终止程序。
下面请看一个完整的程序,用户打开指定的文本文件,读取文件中的 double 类型数据,并在控制台输出所读取数据的数目、总和以及平均数:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; const int SIZE = 60; int main() { char fileName[SIZE]; ifstream InFile; cout<< "Enter the name of data file: " ; cin.getline(fileName, SIZE); cout<<fileName<<endl; 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()){ ++count; sum += value; InFile >> value; } if (InFile.eof()) cout<< "End of file reached:\n" ; else if (InFile.fail()) cout<< "Input terminated by data mismatch.\n" ; else cout<< "Input terminated for unknown reason.\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; } |
总结
到此这篇关于C++中简单的文本文件输入/输出的文章就介绍到这了,更多相关C++文本文件输入/输出内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/francis_xd/article/details/78347595