| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 481 人关注过本帖
标题:程序怎么运行结果不对啊?求指教
只看楼主 加入收藏
meidireshuiq
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2011-9-9
结帖率:85.71%
收藏
已结贴  问题点数:10 回复次数:1 
程序怎么运行结果不对啊?求指教
这是谭浩强c++课本上的程序怎么运行结果不对啊?求指教
#include <iostream>
#include <fstream>
using namespace std;
struct student
{
    int num;
    char name[20];
    float score;
};
int main()
{
    student stud[5]={1001,"li",85,1002,"fun",97.5,1004,"wang",54,1006,"tan",76.5,1010,"ling",96};
    ofstream outfile("stud.dat",ios::out|ios::binary);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        abort();
    }
    ifstream infile("stud.dat",ios::in|ios::binary);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        abort();
    }
    for(int i=0;i<5;i++)
        outfile.write((char *)&stud[i],sizeof(stud[i]));
    student stud1[5];
    for(int i=0;i<5;i=i+2)
    {
        infile.seekg(i*sizeof(stud[i]),ios::beg);
        infile.read((char *)&stud1[i/2],sizeof(stud1[0]));
        cout<<stud1[i/2].num<<" "<<stud1[i/2].name<<" "<<stud1[i/2].score<<endl;
    }
    cout<<endl;
    stud[2].num=1012;
    strcpy(stud[2].name,"wu");
    stud[2].score=60;
    outfile.seekp(2*sizeof(stud[0]),ios::beg);
    outfile.write((char *)&stud[2],sizeof(stud[2]));
    infile.seekg(0,ios::beg);
    for(int i=0;i<5;i++)
    {
        infile.read((char *)&stud[i],sizeof(stud[i]));
        cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
    }
    infile.close();
    outfile.close();
    return 0;
}
搜索更多相关主题的帖子: include using 
2011-10-06 08:47
ragnaros
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:15
专家分:105
注 册:2010-7-26
收藏
得分:10 
程序代码:
#include <iostream>
#include <fstream>
using namespace std;
struct student
{
    int num;
    char name[20];
    float score;
};
int main()
{
    student stud[5]={1001,"li",85,1002,"fun",97.5,1004,"wang",54,1006,"tan",76.5,1010,"ling",96};
    ofstream outfile("stud.dat",ios::out|ios::binary);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        abort();
    }
    ifstream infile("stud.dat",ios::in|ios::binary);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        abort();
    }
    for(int i=0;i<5;i++)
        outfile.write((char *)&stud[i],sizeof(stud[i]));
    student stud1[5];
    for(int j=0;j<5;j=j+2)
    {
        infile.seekg(j*sizeof(stud[j]),ios::beg);
        infile.read((char *)&stud1[j/2],sizeof(stud1[0]));
        cout<<stud1[j/2].num<<" "<<stud1[j/2].name<<" "<<stud1[j/2].score<<endl;
    }
    cout<<endl;
    stud[2].num=1012;
    strcpy(stud[2].name,"wu");
    stud[2].score=60;
    outfile.seekp(2*sizeof(stud[0]),ios::beg);
    outfile.write((char *)&stud[2],sizeof(stud[2]));
    infile.seekg(0,ios::beg);
    for(int k=0;k<5;k++)
    {
        infile.read((char *)&stud[k],sizeof(stud[k]));
        cout<<stud[k].num<<" "<<stud[k].name<<" "<<stud[k].score<<endl;
    }
    infile.close();
    outfile.close();
    return 0;
} 
i重复定义了,将后面两个for循环里的i修改下

[ 本帖最后由 ragnaros 于 2011-10-7 12:15 编辑 ]
2011-10-07 12:14
快速回复:程序怎么运行结果不对啊?求指教
数据加载中...
 
   



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

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