内容2:文本文件的读、写
(1) 新建一个工程demo2,设计一个类Student,其成员变量包括学号(no)、姓名(name)、性别(sex)、专业(major)、联系方式(phone)和地址(addr)。
(2) 为类Student定义成员函数writeTo,实现从给定文本文件中读取学生数据,接口如下:
void writeTo (char filename[ ]);
(3) 为类Student定义成员函数readFrom,实现将学生数据保存到给定文本文件,接口如下:
void readFrom (char filename[ ]);
(4) 主函数的流程为:
a) 定义Student类型的对象stu1和stu2;
b) 输入学生数据,赋值给stu1;
c) 调用writeTo 方法,按照文本格式将stu1的数据保存到stu.txt文件;
d) 调用readFrom 方法,从stu.txt文件中读取学生数据装载到stu2中;
e) 输出stu2的数据。
(5) 程序运行结果如下
内容3:二进制文件的读、写
将实验2中的“文本文件”改为“二进制文件”,其余不变。
此时,程序运行完后,stu.txt文件的内容如下图所示。
示;程序运行完后,stu.txt文件的内容如下右图所示。
附代码
!!!编译环境 DevC++ 5.11 TDM-GCC 4.9.2 64-Bit Release
头文件 iquery.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#ifndef _IQUERY_H #define _IQUERY_H 1 using namespace std; #define MAXN 20 class Student{ public: void input(); friend ostream &operator<<(ostream &,Student &);//重载流运算符<< friend istream &operator>>(istream &,Student &);//重载流运算符>> void writeTo ();//实现从给定文本文件中读取学生数据 void readFrom ();//将学生数据保存到给定文本文件 private: string num; string name; string sex; string phone; string major; string addr; }; #endif
头文件实现文件 iquery,cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#include <iostream> #include "string" #include "iquery.h" #include "iomanip" #include "cmath" #include "fstream" using namespace std; void Student::input(){ cout<<"请输入学生姓名:"<<endl; cin>>this->name; cout<<"请输入学生性别:"<<endl; cin>>this->sex; cout<<"请输入学生学号:"<<endl; cin>>this->num; cout<<"请输入学生专业:"<<endl; cin>>this->major; cout<<"请输入学生联系方式:"<<endl; cin>>this->phone; cout<<"请输入学生住址:"<<endl; cin>>this->addr; } ostream& operator<<(ostream &os,Student &stu){ os<<"学生信息如下:"<<endl; os<<"姓名:"<<stu.name<<endl; os<<"性别:"<<stu.sex<<endl; os<<"专业:"<<stu.num<<endl; os<<"联系方式:"<<stu.phone<<endl; os<<"住址:"<<stu.addr<<endl; return os; } istream &operator>>(istream &is,Student &stu){ cout<<"请输入学生姓名:"<<endl; is>>stu.name; cout<<"请输入学生性别:"<<endl; is>>stu.sex; cout<<"请输入学生学号:"<<endl; is>>stu.num; cout<<"请输入学生专业:"<<endl; is>>stu.major; cout<<"请输入学生联系方式:"<<endl; is>>stu.phone; cout<<"请输入学生住址:"<<endl; is>>stu.addr; return is; } void Student::writeTo (char filename[]){//将学生数据保存到给定文本文件 ofstream outfile; outfile.open(filename); if (!outfile.is_open()) clog<<"error in open student.txt in outfile"; else{ outfile<<this->name<<" "<<this->num<<" "<<this->sex<<" "<<this->phone<<" "<<this->major<<" "<<this->addr; outfile.close(); } } void Student::readFrom (char filename[]){//实现从给定文本文件中读取学生数据 ifstream infile; infile.open(filename); if (!infile.is_open()) clog<<"error in open student.txt in infile"; else{ infile>>this->name;infile.ignore(); infile>>this->num;infile.ignore(); infile>>this->sex;infile.ignore(); infile>>this->phone;infile.ignore(); infile>>this->major;infile.ignore(); infile>>this->addr;infile.ignore(); } }
源码 main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <iostream> #include "string" #include "iquery.h" #include "iomanip" #include "cmath" #include "fstream" using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { Student stu1,stu2; char filename[MAXN] ; cin>>stu1; cout<<"请输入定向输出文件地址"<<endl; cin>>filename; stu1.writeTo(filename); stu2.readFrom(filename); cout<<stu2; return 0; }
二进制输出不再赘述
注:
已经出现过的error:
存在的问题:
可行性优化:
欢迎访问陈风的个人博客
最后
以上就是坚强御姐最近收集整理的关于实验八 输入输出流(二)的全部内容,更多相关实验八内容请搜索靠谱客的其他文章。
发表评论 取消回复