Tao_Gev 2019-06-09 16:59 采纳率: 0%
浏览 456

c++关于读取.txt信息入类中问题

/*
程序功能:事先在记事本上建立包含若干学生记录(包括:学号、姓名、性别、年龄)的文本文件,
编程实现将这些记录读入到内存结构体数组中,并且在屏幕上显示这些记录信息。
/
#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)不是规定了字节大小了吗
图片说明

  • 写回答

1条回答

  • 春秋大漠 2019-06-09 20:18
    关注

    new存储空间
    Student *stu = new Student[10];
    程序结尾处delete:

    if(stu != NULL)
    {
        delete []stu;
        stu = NULL;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器