0
点赞
收藏
分享

微信扫一扫

有五个学生的数据。要求1.把他们存放在磁盘文件中。将磁盘文件中的第1,3,5个学生数据读入程序,并显示出来。将 第三个学生的数据修改后存放回磁盘文件中的原有位置。从磁盘文件读入修改后的数据

ZGtheGreat 2022-05-02 阅读 32
#include<iostream>
#include<fstream>
using namespace std;
struct student
{
	int num;
	char name[30];
	char score;
};
int main()
{
	student stud[5] = { 1001,"li",85,1002,"Fang",97.5,1004,"Wang",56,1006,"Tian",76.5,1010,"ling",96 };
	fstream iofile("stud.dat", ios::in | ios::out | ios::binary);
	if (!iofile)
	{
		cerr << "open error!" << endl;
		abort();
	}
	for (int i = 0; i < 5; i = i ++)
		iofile.write((char*)& stud+, sizeof(stud+));
		student stud[5];
		for (int i = 0; i < 5; i = i + 2)
		{
			iofile.seekg(i * sizeof(stud+), ios::beg);
			iofile.read((char*)& stud[i / 2], sizeof(stud[0]));
			cout << stud[i / 2].num << " " << stud[i / 2].name << " " << stud[i / 2].score << endl;
			cout << endl;
		}
		stud[2].num = 1012;
		strcpy(stud[2].name, "wu");
		stud[2].score = 60;
		iofile.seekp(2 * sizeof(stud[0]), ios::beg);
		iofile.write((char*)& stud[2], sizeof(stud[2]));
		iofile.seekg(0, ios::beg);
		for (int i = 0; i < 5; i++)
		{
			iofile.read((char*)& stud+, sizeof(stud+));
			cout << stud+.num << " " << stud+.name << " " << stud+.score << endl;
		}
		iofile.close();
	system("pause");
	return 0;
}
举报

相关推荐

0 条评论