0
点赞
收藏
分享

微信扫一扫

面向对象程序设计——文件

登高且赋 2022-01-15 阅读 93
c++

一、实验目的:

  1. 掌握流的概念;
  2. 掌握文本文件和二进制文件的使用方法。

二、实验内容:

  1. 建立二进制文件,将CStudent类中数据成员写入文件。并打开文件,读出数据并显示。
    (1) 提示
    参考代码如下:
#include<fstream.h> 
#include<string.h>
#include<iomanip.h>

class CStudent //定义学生类
{
	char name[20];
	int number;
	double english;
	double math;
public:
	CStudent(char* str="",int num=0,double en=0,double ma=0);//构造函数
	void display();  //显示数据成员
	datatofile(ofstream &out);//把数据写入文件 
	datafromfile(ifstream &in);//从文件读出数据
};

CStudent::CStudent(char *str,int num,double en,double ma)
{
	strcpy(name,str);
	number=num;
	english=en;
	math=ma;
}
void CStudent::display()
{
	cout.setf(ios::left);
	cout<<setw(20)<<name;
	cout.unsetf(ios::left); 
	cout.setf(ios::right); //要改为右对齐,先清左对齐
	cout<<setw(10)<<number<<setw(10)<<english<<setw(10)<<math<<endl;
}

CStudent::datatofile(ofstream &out)
{
	out.write(name,20);
	out.write((char*)&number,sizeof(int));
	out.write((char*)&english,sizeof(double));
	out.write((char*)&math,sizeof(double));
}

CStudent::datafromfile(ifstream &in) 
{
	in.read(name,20);
	in.read((char*)&number,sizeof(int));
	in.read((char*)&english,sizeof(double));
	in.read((char*)&math,sizeof(double));
}

void main()
{
	CStudent stu1("张三",200401003,97.6,84);
	CStudent stu2("李四",200508298,83,76.5);
	ofstream dtof("s.dat",ios::out|ios::binary);
	stu1.datatofile(dtof);
	stu2.datatofile(dtof);
	cout<<"学生1:"<<endl;
	stu1.display();
	cout<<"学生2:"<<endl;
	stu2.display();
	dtof.close();
	cout<<endl;

	CStudent s1;
	int i=0;
	ifstream dfromf("s.dat",ios::in|ios::binary);//重新打开文件,从头读取数据
	while(1)
	{
		s1.datafromfile(dfromf);//从文件读取数据拷贝到对象s1
		i++;
		if(dfromf.eof()!=0)break;
		cout<<"第"<<i<<"个学生:"<<endl;
		s1.display();
		cout<<"读文件成功"<<endl; 
	}

	dfromf.close();
}

⑵要求

调试代码,实现对二进制文件的读取;修改程序,实现从键盘上输入每个记录的数据成员;
在这里插入图片描述
修改后的程序(主要修改了main函数):

#include <fstream>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <iostream>
using namespace std;

class CStudent //定义学生类
{
	char name[20];
	int number;
	double english;
	double math;
public:
	CStudent(char* str="",int num=0,double en=0,double ma=0);//构造函数
	void display();  //显示数据成员
	datatofile(ofstream &out);//把数据写入文件
	datafromfile(ifstream &in);//从文件读出数据
};

CStudent::CStudent(char *str,int num,double en,double ma)
{
	strcpy(name,str);
	number=num;
	english=en;
	math=ma;
}
void CStudent::display()
{
	cout.setf(ios::left);
	cout<<setw(20)<<name;
	cout.unsetf(ios::left);
	cout.setf(ios::right); //要改为右对齐,先清左对齐
	cout<<setw(10)<<number<<setw(10)<<english<<setw(10)<<math<<endl;
}

CStudent::datatofile(ofstream &out)
{
	out.write(name,20);
	out.write((char*)&number,sizeof(int));
	out.write((char*)&english,sizeof(double));
	out.write((char*)&math,sizeof(double));
}

CStudent::datafromfile(ifstream &in)
{
	in.read(name,20);
	in.read((char*)&number,sizeof(int));
	in.read((char*)&english,sizeof(double));
	in.read((char*)&math,sizeof(double));
}

int main()
{
	char name1[20];
	int number1;
	double english1;
	double math1;
	cout<<"请输入学生一的信息:"<<endl;
	cin>>name1>>number1>>english1>>math1;
	CStudent stu1(name1,number1,english1,math1);
	cout<<"请输入学生二的信息:"<<endl;
	cin>>name1>>number1>>english1>>math1;
	CStudent stu2(name1,number1,english1,math1);
	ofstream dtof("s.dat",ios::out|ios::binary);
	stu1.datatofile(dtof);
	stu2.datatofile(dtof);
	cout<<"学生1:"<<endl;
	stu1.display();
	cout<<"学生2:"<<endl;
	stu2.display();
	dtof.close();
	cout<<endl;

	CStudent s1;
	int i=0;
	ifstream dfromf("s.dat",ios::in|ios::binary);//重新打开文件,从头读取数据
	while(1)
	{
		s1.datafromfile(dfromf);//从文件读取数据拷贝到对象s1
		i++;
		if(dfromf.eof()!=0)break;
		cout<<"第"<<i<<"个学生:"<<endl;
		s1.display();
		cout<<"读文件成功"<<endl;
	}

	dfromf.close();

	return 0;
}

在这里插入图片描述

  1. 实现对第1题中的二进制s.dat文件的随机读取。

(1) 提示

class CStudent //定义学生类
{
	char note[20];
	char name[20];
	int number;
	double english;
	double math;
public:
	CStudent(char *re="",char* str="",int num=0,double en=0.0,double ma=0.0); 
//构造函数
	void display();  //显示数据成员
	datatofile(ofstream &out);//把数据写入文件 
	datafromfile(ifstream &in);//从文件读出数据
};

⑵ 要求

(1) 二进制文件s.dat中的记录为:
第1条记录 张三 200401003 97.6 84
第2条记录 李四 200508298 83 76.5
第3条记录 王五 200533005 45 70.5

(2) 设置写指针,把如下第4条记录插到第1条记录之后,即第4条记录覆盖第2条记录
第4条记录 二马 200533456 85 60

(3) 重新打开文件s.dat,从头读取数据,并在屏幕上输出;

(4) 重新打开文件,设置读指针,读取文件中的第2条记录,并在屏幕上输出。
实验输出结果如下图所示:
在这里插入图片描述
注意:如果用fstream进行文件的输入/输出时,文件定位指针只有一个,输入输出都是用此定位指针。

学习掌握

流的概念及应用,每个流都和一种设备相联系,与输入设备(如键盘)联系的流为输入流;与输出设备(如屏幕)联系的流为输出流;包括标准输入输出流、缓冲和非缓冲流等。
文件的打开关闭还有数据的读取与写入,输出格式的控制。

举报

相关推荐

0 条评论