/*
程序功能:事先在记事本上建立包含若干学生记录(包括:学号、姓名、性别、年龄)的文本文件,
编程实现将这些记录读入到内存结构体数组中,并且在屏幕上显示这些记录信息。
/
#include
#include
using namespace std;
struct Student
{
char id[6], name[4],sex[1];
int age;
};
ostream & operator << (ostream &out,const Student &a)
{
out<<"Id : "<<a.id<<" Name : "<<a.name<<" Sex : "<<a.sex<<" Age : "<<a.age<<endl;
return out;
}
int main()
{
Student stu[10];
ifstream inf("studata.txt");
if(!inf)
{
cout<<"The file can't open\n";
}
int i=0;
while(!inf.eof())
{
inf.read((char *)&stu[i++],sizeof(Student));
}
//cout<<stu[0];
for(int j=0;j<3;j++)
{
cout<<stu[j];
}
inf.close();
system("pause");
return 0;
}
//以下为文本数据
/
Y2018 hht m 20
Y2017 zzh m 20
Y2019 ggy m 20
*/
//大佬们,哪里出错了,只输出stu[0]就出现了很多数据,sizeof(Student)不是规定了字节大小了吗
c++关于读取.txt信息入类中问题
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 春秋大漠 2019-06-09 20:18关注
new存储空间
Student *stu = new Student[10];
程序结尾处delete:if(stu != NULL) { delete []stu; stu = NULL; }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 保护模式-系统加载-段寄存器
- ¥15 电脑桌面设定一个区域禁止鼠标操作
- ¥15 求NPF226060磁芯的详细资料