| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 827 人关注过本帖
标题:存储的fwrite()问题
只看楼主 加入收藏
yijing21
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-11-7
收藏
 问题点数:0 回复次数:2 
存储的fwrite()问题
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++中存储数据到文件一般用什么方法啊 ?   急等解答。。
搜索更多相关主题的帖子: fwrite 
2008-04-16 11:06
herolzx
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-25
收藏
得分:0 
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]]
2008-04-16 11:19
yijing21
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-11-7
收藏
得分:0 
哦。。。忘了  给stream初始化   多谢楼上提醒
2008-04-16 11:55
快速回复:存储的fwrite()问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017761 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved