文件输入学生信息终极版

#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class student
{
public:
	int getClassid()
	{
		return classID;
	}
	void setClassid(int cl_id)
	{
		classID = cl_id;
	}
	int getid()
	{
		return id;
	}
	void setid(int s_id)
	{
		id = s_id;
	}
	string getName()	
	{
		return name;
	}
	void setName(string _name)
	{
		name = _name;
	}
private:
	int classID;//年级
	int id;//学号
	string name;
};

int main()
{
	student st[3];
	int classid, id;
	string name;
	cout << "需要输入三个学生的信息" << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << "输入第" << i + 1 << "个学生所在班级:" << endl;
		cin >> classid;
		cout << "输入第" << i + 1 << "个学生学号:" << endl;
		cin >> id;
		cout << "输入第" << i + 1 << "个学生姓名:" << endl;
		cin >> name;

		st[i].setClassid(classid);
		st[i].setid(id);
		st[i].setName(name);
	}
	ofstream os;
	os.open("d:\\a.txt");
	if (os)
	{
		os << "班级      " << "学号       " << "姓名" << endl;
		for (int i = 0; i < 3; i++)
		{
			os << st[i].getClassid() <<"      " << st[i].getid() << "       " << st[i].getName();
			if (i <= 1)os << endl;
		}

		os.close();
		cout << "写入数据成功!" << endl;
	}
	else
	{
		cout << "打开文件错误!" << endl;
	}

	char line[1024];
	ifstream is;
	is.open("d:\\a.txt");
	int i = 0;
	if(is)
	{
		is.getline(line,1024);
		while (!is.eof() && i < 3)
		{
			is >> classid >> id >> name;
			st[i].setClassid(classid);
			st[i].setid(id);
			st[i].setName(name);
			i++;
		}

		is.close();
		cout << "读入数据成功!" << endl;

	}
	else
	{
		cout << "打开文件错误!" << endl;
	}
	cout << "班级      " << "学号       " << "姓名" << endl;
	for (int i = 0; i < 3; i++)
	{
		cout << st[i].getClassid() << "      " << st[i].getid() << "       " << st[i].getName() << endl;
	}

	system("pause");
    return 0;
}
老师版本,终极的终极版


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值