实验八 输入输出流(二)

内容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

#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

#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

#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:
存在的问题:
可行性优化:
欢迎访问陈风的个人博客

  • 14
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值