课程设计编了个学生管理系统,在保存读取数据时,如果是保存后关闭程序再打开程序就读出很奇怪的东西,但是如果不关程序直接,先保存数据,再删除数据,然后读取数据就没问题,各位大神帮看看~~
void CStudentDlg::OnButton17() //保存信息
{
// TODO: Add your control notification handler code here
int nCount1 = m_ptrArray1.GetSize();
int nCount2 = m_ptrArray2.GetSize();
int nCount3 = m_ptrArray3.GetSize();
CLesson* lesson = new CLesson[nCount1];
CStudent* student = new CStudent[nCount2];
CGrade* grade = new CGrade[nCount3];
for(int i1 = 0;i1 < nCount1;i1++){
lesson[i1] = *(CLesson*)m_ptrArray1.GetAt(i1); //把m_ptrArray1存入CLesson指针数组中
}
for(int i2 = 0;i2 < nCount2;i2++){
student[i2] = *(CStudent*)m_ptrArray2.GetAt(i2); //把m_ptrArray2存入CStudent指针数组中
}
for(int i3 = 0;i3 < nCount3;i3++){
grade[i3] = *(CGrade*)m_ptrArray3.GetAt(i3); //把m_ptrArray3存入CGrade指针数组中
}
ofstream fout ("data.txt",ios::out|ios::binary); //用二进制写入方式
fout.write((char *)&nCount1,sizeof(nCount1)); //对总数进行保存
fout.write((char *)&nCount2,sizeof(nCount2));
fout.write((char *)&nCount3,sizeof(nCount3));
for(int i4 = 0;i4 < nCount1;i4++){
fout.write( (char*)&lesson[i4],sizeof(lesson[i4]) ); //把CLesson指针数组存入data.txt文件中
}
for(int i5 = 0;i5 < nCount1;i5++){
fout.write( (char*)&student[i5],sizeof(student[i5]) ); //把CStudent指针数组存入data.txt文件中
}
for(int i6 = 0;i6 < nCount1;i6++){
fout.write( (char*)&grade[i6],sizeof(grade[i6]) ); //把CGrade指针数组存入data.txt文件中
}
fout.close();
MessageBox("成功将文件保存在data.txt");
}
void CStudentDlg::OnButton18() //读取信息
{
// TODO: Add your control notification handler code here
int nCount1;
int nCount2;
int nCount3;
ifstream fin("data.txt",ios::in|ios::binary);
fin.read((char*)&nCount1,sizeof(nCount1)); //读取总数
fin.read((char*)&nCount2,sizeof(nCount2));
fin.read((char*)&nCount3,sizeof(nCount3));
CLesson* lesson =new CLesson[nCount1];
CStudent* student =new CStudent[nCount2];
CGrade* grade =new CGrade[nCount3];
for(int i1 = 0;i1 < nCount1;i1++){
fin.read( (char*)&lesson[i1],sizeof(lesson[i1]) ); //将data.txt文件读取到lesson指针数组中
}
for(int i2 = 0;i2 < nCount2;i2++){
fin.read( (char*)&student[i2],sizeof(student[i2]) ); //将data.txt文件读取到student指针数组中
}
for(int i3 = 0;i3 < nCount3;i3++){
fin.read( (char*)&grade[i3],sizeof(grade[i3]) ); //将data.txt文件读取到grade指针数组中
}
fin.close();
for(int i4 = 0;i4 < nCount1;i4++){
m_ptrArray1.Add(&lesson[i4]); //将lesson指针数组保存到m_ptrArray1中
}
for(int i5 = 0;i5 < nCount2;i5++){
m_ptrArray2.Add(&student[i5]); //将student指针数组保存到m_ptrArray2中
}
for(int i6 = 0;i6 < nCount3;i6++){
m_ptrArray3.Add(&grade[i6]); //将grade指针数组保存到m_ptrArray3中
}
MessageBox("成功读取文件data.txt");
}
void CStudentDlg::OnButton17() //保存信息
{
// TODO: Add your control notification handler code here
int nCount1 = m_ptrArray1.GetSize();
int nCount2 = m_ptrArray2.GetSize();
int nCount3 = m_ptrArray3.GetSize();
CLesson* lesson = new CLesson[nCount1];
CStudent* student = new CStudent[nCount2];
CGrade* grade = new CGrade[nCount3];
for(int i1 = 0;i1 < nCount1;i1++){
lesson[i1] = *(CLesson*)m_ptrArray1.GetAt(i1); //把m_ptrArray1存入CLesson指针数组中
}
for(int i2 = 0;i2 < nCount2;i2++){
student[i2] = *(CStudent*)m_ptrArray2.GetAt(i2); //把m_ptrArray2存入CStudent指针数组中
}
for(int i3 = 0;i3 < nCount3;i3++){
grade[i3] = *(CGrade*)m_ptrArray3.GetAt(i3); //把m_ptrArray3存入CGrade指针数组中
}
ofstream fout ("data.txt",ios::out|ios::binary); //用二进制写入方式
fout.write((char *)&nCount1,sizeof(nCount1)); //对总数进行保存
fout.write((char *)&nCount2,sizeof(nCount2));
fout.write((char *)&nCount3,sizeof(nCount3));
for(int i4 = 0;i4 < nCount1;i4++){
fout.write( (char*)&lesson[i4],sizeof(lesson[i4]) ); //把CLesson指针数组存入data.txt文件中
}
for(int i5 = 0;i5 < nCount1;i5++){
fout.write( (char*)&student[i5],sizeof(student[i5]) ); //把CStudent指针数组存入data.txt文件中
}
for(int i6 = 0;i6 < nCount1;i6++){
fout.write( (char*)&grade[i6],sizeof(grade[i6]) ); //把CGrade指针数组存入data.txt文件中
}
fout.close();
MessageBox("成功将文件保存在data.txt");
}
void CStudentDlg::OnButton18() //读取信息
{
// TODO: Add your control notification handler code here
int nCount1;
int nCount2;
int nCount3;
ifstream fin("data.txt",ios::in|ios::binary);
fin.read((char*)&nCount1,sizeof(nCount1)); //读取总数
fin.read((char*)&nCount2,sizeof(nCount2));
fin.read((char*)&nCount3,sizeof(nCount3));
CLesson* lesson =new CLesson[nCount1];
CStudent* student =new CStudent[nCount2];
CGrade* grade =new CGrade[nCount3];
for(int i1 = 0;i1 < nCount1;i1++){
fin.read( (char*)&lesson[i1],sizeof(lesson[i1]) ); //将data.txt文件读取到lesson指针数组中
}
for(int i2 = 0;i2 < nCount2;i2++){
fin.read( (char*)&student[i2],sizeof(student[i2]) ); //将data.txt文件读取到student指针数组中
}
for(int i3 = 0;i3 < nCount3;i3++){
fin.read( (char*)&grade[i3],sizeof(grade[i3]) ); //将data.txt文件读取到grade指针数组中
}
fin.close();
for(int i4 = 0;i4 < nCount1;i4++){
m_ptrArray1.Add(&lesson[i4]); //将lesson指针数组保存到m_ptrArray1中
}
for(int i5 = 0;i5 < nCount2;i5++){
m_ptrArray2.Add(&student[i5]); //将student指针数组保存到m_ptrArray2中
}
for(int i6 = 0;i6 < nCount3;i6++){
m_ptrArray3.Add(&grade[i6]); //将grade指针数组保存到m_ptrArray3中
}
MessageBox("成功读取文件data.txt");
}