我做了个简单的程序,可以向里面输入学生的信息,比如10个学生的年龄,姓名,性别,学号,通过学号就可以查询这个学生的其他的信息。我现在是想把这些信息保存下来,下回再启动这个程序时可以调用上次已保存的内容,不必再重新初始化学生信息。我用了一些文件操作,其中保存和调用的函数如下:
保存文件的函数:
void INPUT::OnSave() 
{
// TODO: Add your control notification handler code here
int i;
CFile f;
f.Open("D:\\SAVE.TRY",CFile::modeCreate|CFile::modeWrite);
CArchive ar(&f,CArchive::store);
for(i=0;i<10;i++){
ar<<ourstudent[i].sage<<ourstudent[i].sname<<ourstudent[i].snumber<<ourstudent[i].ssex;}
ar.Close();
f.Close();
}
调用文件的函数:
void SEARCH::OnLode() 
{
// TODO: Add your control notification handler code here
int i;
CFile f;
if(f.Open("D:\\SAVE.TRY",CFile::modeRead)==FALSE)
return;
CArchive ar(&f,CArchive::load);
for(i=0;i<10;i++)
{ar>>ourstudent[i].snumber>>ourstudent[i].sname>>ourstudent[i].sage>>ourstudent[i].ssex;}
ar.Close();
f.Close();}
其中ourstudent[]为一个10个人的结构体数组,我原意是在初始化学生信息完毕(10个学生的信息全部存入了ourstudent[]中)然后通过按钮调用onsave函数,下次运行程序时再通过onlode函数来调用上次储存的信息,但是虽然程序编译通过了,在实际运行时却不能实现我希望的储存和提取功能,请各位指教!!非常感谢!!

解决方案 »

  1.   

    你在什么地方调用这两个函数的?另外,你的sage和snumber在存取时的顺序不一样。
      

  2.   

    ar<<ourstudent[i].sage<<ourstudent[i].sname<<ourstudent[i].snumber<<ourstudent[i].ssex;}
    ar>>ourstudent[i].snumber>>ourstudent[i].sname>>ourstudent[i].sage>>ourstudent[i].ssex;}
    为什么snumber和sage的位置变了?
      

  3.   

    调整顺序一致.
    除非ourstudent没放数据
      

  4.   

    CSting mm.
    mm.format
    先把所有的值都转化成CSting类型,其实写进去了,只不过你看不到,可能是按阿斯科码值写进去的
      

  5.   

    CString aa;
    aa.Format(格式..)
    aa.WriteString(mm)
    .
    .
    .
    每种数据都将格式转换过来!
      

  6.   

    CFile f;
     int i=47;
    char buffer[512];
    f.Open("D:\\SAVE.txt",CFile::modeCreate|CFile::modeWrite);
    CArchive ar(&f,CArchive::store,512,buffer);
    if(!ar.IsStoring())
    MessageBox("wrong");
    CString mm;
    mm.Format("%d",i);
    ar<<mm; ar.Close();
    f.Close();
      

  7.   

    char buff[100]={0};
    FILE *fp;
    fp=fopen("ttt.txt","wt");
    fprintf(fp,buff,10);
    fclose(fp);
      

  8.   

    跟你的数据结构有关,如定义为CString 类型,
    写入时,只写入4个字节的地址,建议改为char*型。