文件

当向文件写入和读取的时候我们就需要用到ifstream、ofstream、fstream,而这三个数据类型都来自fstream这个标准库,下面我们看下他们具体的用法。

数据类型描述
ofstream 该数据类型表示输出文件流,用于创建文件并向文件写入信息。
ifstream 该数据类型表示输入文件流,用于从文件读取信息。
fstream 该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。

要在 C++ 中进行文件处理,必须在 C++ 源代码文件中包含头文件 <iostream> 和 <fstream>。

打开文件:在我们打开文件前我们先要定义打开文件的数据类型 ifstream file; 然后利用open函数打开文件,open() 函数是 fstream、ifstream 和 ofstream 对象的一个成员。 open() 函数的标准语法 void open(const char *filename, ios::openmode mode); 其中第一个参数是我们文件的路径和名称,第二个参数是我们操作的模式,具体为:

模式标志描述
ios::app 追加模式。所有写入都追加到文件末尾。
ios::ate 文件打开后定位到文件末尾。
ios::in 打开文件用于读取。
ios::out 打开文件用于写入。
ios::trunc 如果该文件已经存在,其内容将在打开文件之前被截断,即把文件长度设为 0。

如:

ifstream file;
file.open("a.txt",ios::app);

写入数据:在 C++ 编程中,我们使用流插入运算符( << )向文件写入信息,就像使用该运算符输出信息到屏幕上一样。唯一不同的是,在这里您使用的是 ofstream 或 fstream 对象,而不是 cout 对象。如: file << "hello world " <<endl;

读取数据:在 C++ 编程中,我们使用流提取运算符( >> )从文件读取信息,就像使用该运算符从键盘输入信息一样。唯一不同的是,在这里您使用的是 ifstream 或 fstream 对象,而不是 cin 对象。 如: char a[100]; file >> a >> endl; 

关闭文件:当 C++ 程序终止时,它会自动关闭刷新所有流,释放所有分配的内存,并关闭所有打开的文件。但我们应该养成一个好习惯,在程序终止前关闭所有打开的文件。close() 函数是 fstream、ifstream 和 ofstream 对象的一个成员。如: file.close(); 

下面写了个实例,具体用到了继承、数据抽象,抽象类、多态。基本上把面向对象的知识都用了下。

  1 #include <iostream>
  2 #include <fstream>
  3 #include <time.h>
  4 using namespace std;
  5 class StudentInfo
  6 {
  7 public:
  8     StudentInfo(double tMath, double tChiness, double tEnglish)
  9     {
 10         resu = 0;
 11         math = tMath;
 12         chiness = tChiness;
 13         english = tEnglish;
 14     }
 15     StudentInfo() {}
 16     virtual void InInfo(char name[10]) {}
 17     void ReInName(char name[10])//让数据和实现细节分离
 18     {
 19         InName(name);
 20     }
 21 protected:
 22     double resu;
 23     double math;
 24     double chiness;
 25     double english;
 26     //向文本中插入姓名
 27     void InName(char name[10])
 28     {
 29         ofstream filein;
 30         filein.open("studentinfo.txt");
 31         filein << "学生姓名:" << name << endl;
 32         filein.close();
 33     }
 34 };
 35 //用来得到总成绩
 36 class StudentAchieSum : public StudentInfo
 37 {
 38 public:
 39     ofstream filein;
 40     using StudentInfo::StudentInfo;//隐式集成构造函数
 41     void InInfo(char name[10])
 42     {
 43         filein.open("studentinfo.txt", ios::app);
 44         resu = math + chiness + english;
 45         filein << "   总成绩:" << resu << endl;
 46         filein.close();
 47     }
 48 };
 49 //用来得到平均成绩
 50 class StudentAchiePv : public StudentInfo
 51 {
 52 public:
 53     ofstream filein;
 54     StudentAchiePv(double tMath, double tChiness, double tEnglish) :StudentInfo( tMath, tChiness, tEnglish) {};//显式继承构造函数
 55     void InInfo(char name[10])
 56     {
 57         filein.open("studentinfo.txt", ios::app);
 58         resu = (math + chiness + english)/3;
 59         filein << "平均成绩:" << resu << endl;
 60         filein.close();
 61     }
 62 };
 63 //抽象类
 64 class VirStudeng
 65 {
 66 public:
 67     virtual void InInfoId() = 0; //纯虚函数
 68 protected:
 69     int resu;
 70 };
 71 //用来得到学生的ID
 72 class StudentId :public VirStudeng
 73 {
 74 public:
 75     void InInfoId()
 76     {
 77         ofstream filein;
 78         srand((int)time(0));//种下随机数种子
 79         resu = rand() % 100000;//得到随机数
 80         filein.open("studentinfo.txt", ios::app);//打开文件
 81         filein << "学生编号:" << resu << endl;//插入数据
 82         filein.close();
 83     }
 84 };
 85 int main()
 86 {
 87     //定义变量
 88     char name[10] = "";
 89     double math = 0;
 90     double chiness = 0;
 91     double english = 0;
 92     //获取变量值
 93     cout << "请输入学生姓名:" ;
 94     cin >> name;
 95     cout << "欢迎您" << name << "!"  << "请您输入你的成绩:" << endl;
 96     cout << "数学:";
 97     cin >> math;
 98     cout << "语文:";
 99     cin >> chiness;
100     cout << "英语:";
101     cin >> english;
102     //文本中插入姓名
103     StudentInfo std;
104     std.ReInName(name);
105     //文本中插入编号
106     StudentId sId;
107     sId.InInfoId();
108     //文本中插入成绩,这里用到了多态
109     StudentInfo *stdinfo;
110     StudentAchieSum Sum(math, chiness, english);//初始化构造函数
111     stdinfo = &Sum;
112     stdinfo->InInfo(name);
113     StudentAchiePv Pv(math, chiness, english);//初始化构造函数
114     stdinfo = &Pv;
115     stdinfo->InInfo(name);
116     cout << "输入完毕,请按ESC键推出。" << endl;
117     system("pause");
118     return 0;
119 }

输入如下:

然后我们从根目录下找到【studentinfo.txt】这个文件,打开以后显示:

学生姓名:小党先生
学生编号:18579
总成绩:284
平均成绩:94.6667

原文地址:https://www.cnblogs.com/xiaodangxiansheng/p/11003938.html