| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 319 人关注过本帖
标题:关于文件读写的问题
只看楼主 加入收藏
wx1144542900
Rank: 2
等 级:论坛游民
帖 子:24
专家分:10
注 册:2013-11-1
结帖率:100%
收藏
 问题点数:0 回复次数:1 
关于文件读写的问题
程序代码:
/*检索错误,查找错误,编号姓名不可重复输入,姓名不能位数字,成绩不能小于0,无文件输入输出,无重载运算符*/


#include <iostream>
#include<iomanip>
#include <string>
#include<fstream>
using namespace std;

class CStudent
{
//private:                        //友元无法访问私有成员
public:
    int num;                    //编号
    string name;                //姓名
    char sex;                    //性别
    int age;                    //年龄
    int math;                    //数学成绩
    int computer;                //计算机成绩
    int English;                //英语成绩
public:
    CStudent(int i=0,string b1="jii",char j='p',int d=0,int e1=0,int f=0,int g=0):num(i),name(b1),sex(j),age(d),math(e1),computer(f),English(g){}
    void input(int);
    int displaynum(CStudent &t,int a);                    //检索错误!!!!!!!!!!!!!
    int displayname(CStudent &t,string b);                //检索错误!!!!!!!!!!!!!
    int dispalylist(CStudent &t,int n,char s);            //错误错误错误
    int SumScore();
    double Ave();
    //friend ostream& operator << (ostream &out,CStudent &t);
} ;
//计算三门总成绩
int CStudent::SumScore()
{
    return math+computer+English;
}
//计算平均分
double CStudent::Ave()
{
    return this->SumScore()/3;
}
//输入学生信息
void CStudent::input(int i)
{
    string b1;
    char j;
    int d;
    int e1;
    int f;
    int g;
    cout<<"输入学生信息"<<endl;
    cout<<"姓名:";
    cin>>b1;
    cout<<"性别:";
    cin>>j;
    cout<<"年龄:";
    cin>>d;
    cout<<"数学成绩:";
    cin>>e1;
    cout<<"计算机成绩:";
    cin>>f;
    cout<<"英语成绩:";
    cin>>g;
    num=i;name=b1;sex=j;age=d;math=e1;computer=f;English=g;
}

//按编号检索
int CStudent::displaynum(CStudent &t,int a )
{
    if(t.num==a)
    {
        cout<<"学号   姓名   性别    年龄  数学成绩  计算机成绩  英语成绩  总分    平均分"<<endl;
        cout<<num<<setw(7);
        cout<<name<<setw(7);
        cout<<sex<<setw(7);
        cout<<age<<setw(7);
        cout<<math<<setw(11);
        cout<<computer<<setw(11);
        cout<<English<<setw(11)<<t.SumScore()<<setw(9)<<t.Ave()<<endl;
        return 1;
    }
    return 0;
}

ostream& operator << (ostream &out,CStudent &t)
{
    out<<"学号   姓名   性别    年龄  数学成绩  计算机成绩  英语成绩  总分    平均分"<<endl;
    out<<t.num<<setw(7);
    out<<t.name<<setw(7);
    out<<t.sex<<setw(7);
    out<<t.age<<setw(7);
    out<<t.math<<setw(11);
    out<<(11);
    out<<t.English<<setw(11)<<t.SumScore()<<setw(9)<<t.Ave()<<endl;
    return out;
}

//按姓名检索
int CStudent::displayname(CStudent &t,string b)
{
    if(t.name==b)
    {
    
        cout<<"学号   姓名   性别    年龄  数学成绩  计算机成绩  英语成绩  总分    平均分"<<endl;
        cout<<num<<setw(7);
        cout<<name<<setw(7);
        cout<<sex<<setw(7);
        cout<<age<<setw(7);
        cout<<math<<setw(11);
        cout<<computer<<setw(11);
        cout<<English<<setw(11)<<t.SumScore()<<setw(9)<<t.Ave()<<endl;
        return 1;
    }
    else    cout<<"没有找到该学生"<<endl;
    return 0;
}

//列出所有总成绩超过n分的性别为s同学的有关信息
int CStudent::dispalylist(CStudent &t,int n,char s)
{
    if (t.math+ && t.sex==s)
    {
        cout<<"学号   姓名   性别    年龄  数学成绩  计算机成绩  英语成绩  总分    平均分"<<endl;
        cout<<num<<setw(7);
        cout<<name<<setw(7);
        cout<<sex<<setw(7);
        cout<<age<<setw(7);
        cout<<math<<setw(11);
        cout<<computer<<setw(11);
        cout<<English<<setw(11)<<t.SumScore()<<setw(9)<<t.Ave()<<endl;
        return 1;
    }
    else cout<<"没有找到该学生"<<endl;
    return 0;
}


int main()
{
    int a,n,d,i,j,k,l;
    string b;
    char s;
    CStudent *st;
    cout<<"输入学生个数"<<endl;
    cin>>d;                            //d为学生个数
    st=new CStudent[d];
    for(i=0;i<d;i++)
        st[i].input(i+1);

    //写入文件
    ofstream outfile("student.dat",ios::binary);
    if(!outfile)
    {
        cerr<<"open error!";
        return 0;
    }
    for(int m=0;m<d;m++)
        outfile.write((char*)&st[i].sizeof(st[i]));    
    outfile.close( );                            //怎么回事,出错了?????????

    int select=0;
    cout<<"按1按编号搜索,按2按姓名搜索,按3所有总成绩超过n分的性别为s同学的有关信息(n,s由用户从键盘输入) "<<endl;
    cout<<"如果想退出程序请按4"<<endl;

    while(select!=4)
    {
        cin>>select;
        switch (select)
        {
        case 1: 
            cout<<"输入搜索学生编号"<<endl;
            cin>>a;                    //a为学生编号
            for(j=0;j<d;j++)
                if(st[j].displaynum(st[j],a))
                    break;
            break;        
        case 2:
            cout<<"输入搜索学生姓名"<<endl;
            cin>>b;                    //b为学生姓名
            for(k=0;k<d;k++)
                if(st[k].displayname(st[k],b))
                    break;
            break;

        case 3:
            cout<<"输入搜索学生总成绩和性别"<<endl;
            cin>>n>>s;
            for(l=0;l<d;l++)
                if(st[i].dispalylist(st[i],n,s))
                    break;
            break;
                
        }

    }
    return 0;
}



好吧,错误很多,希望高手指点一二,特别是对于文件的写入,我感觉应该是没有问题的,问题出哪了,谢谢指点
2013-12-21 17:38
IT男year
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:82
专家分:106
注 册:2013-6-9
收藏
得分:0 
outfile.write((char*)&st[i].sizeof(st[i]));  
少年这句应该是这样的----------》outfile.write((char*)&st[i],sizeof(st[i]));
2013-12-22 11:21
快速回复:关于文件读写的问题
数据加载中...
 
   



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

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