/*
程序功能:事先在记事本上建立包含若干学生记录(包括:学号、姓名、性别、年龄)的文本文件,
编程实现将这些记录读入到内存结构体数组中,并且在屏幕上显示这些记录信息。
/
#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)不是规定了字节大小了吗
new存储空间
Student *stu = new Student[10];
程序结尾处delete:
if(stu != NULL)
{
delete []stu;
stu = NULL;
}