注册 登录
编程论坛 C++教室

存储的fwrite()问题

yijing21 发布于 2008-04-16 11:06, 832 次点击
struct student{                                        
    int num;
    string name;
    int score[3];
};
void Information::save_file()
{
    student stu;
    string name;
    FILE *stream;
    fopen("stu.dat","wb+");
    int score;
    int num;
    
    cout<<"请输入8位学号:";
    cin>>stu.num;

    cout<<"\n请输入名字:";
    cin>>stu.name;
    
    cout<<"\n请输入数学成绩";
    cin>>stu.score[0];
    cout<<"请输入英语成绩";
    cin>>stu.score[1];

    
    cout<<"请输入政治成绩";
    cin>>stu.score[2];
    
    fwrite(&stu,sizeof(struct student),1,stream);
    fclose(stream);
    
}

int main()
{
    Information IF;
    IF.save_file();
    return 0;
}


为什么程序在对其输入3门成绩后显示运行出错了  是不是fwrite()应用出问题了  顺便问一句  是不是C++中存储数据到文件一般用什么方法啊 ?   急等解答。。
2 回复
#2
herolzx2008-04-16 11:19
struct student{                                       
    int num;
    string name;
    int score[3];
};
void Information::save_file()
{
    student stu;
    string name;
    FILE *stream;//stream没有初始化  
    stream = fopen("stu.dat","wb+");
    assert(stream != 0);
.....

[[it] 本帖最后由 herolzx 于 2008-4-16 11:22 编辑 [/it]]
#3
yijing212008-04-16 11:55
哦。。。忘了  给stream初始化   多谢楼上提醒
1