| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 722 人关注过本帖
标题:各位!!看看好奇怪为什么写进去的文件读取时老出错,当吧数据类型由string ...
只看楼主 加入收藏
芦浩轩
Rank: 1
来 自:河北
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-25
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:6 
各位!!看看好奇怪为什么写进去的文件读取时老出错,当吧数据类型由string换成int就能正常读取。。求解答
#include<iostream>
#include<string>
#include<time.h>
#include<stdio.h>
#include<fstream>
using namespace std;

time_t timep;
struct tm *p;
class People
{
public:
    string m_Name;
    string m_Sex;
    int m_Age;
    int m_Hour;
    int m_Mine;
    int m_Sec;
   
public:
   
//    People(){}//默认构造函数
    People(string name="_",int age=0,string sex="_",int h=0,int m=0,int s=0):m_Name(name),m_Age(age),m_Sex(sex),m_Hour(h),m_Mine(m),m_Sec(s){}//带参的默认构造函数-形参列表-内联形式
    virtual void RegistMessage();
    virtual void ShowMessage();//类外定义形式
    virtual ostream& operator<<(ostream& output);//输出,重载函数
    ~People(){}//析构函数--内联
};
ostream& People::operator <<(ostream& output)
{
    puts("签到时间:");
        output<<m_Hour+8<<":"<<m_Mine<<":"<<m_Sec<<endl;
         return output;
}
void People::ShowMessage()
{
    cout<<"姓名\t年龄\t性别"<<endl;
    cout<<m_Name<<"\t"<<m_Age<<"\t"<<m_Sex<<endl;

   
}
void People::RegistMessage()
{
    cout<<"请输入名字"<<endl;
    cin>>this->m_Name;
    cout<<"请输入年龄"<<endl;
    cin>>this->m_Age;
    if(m_Age>=100||m_Age<=15)
        //throw Exception("Error!!不合法的输入操作!!");
    cout<<"请输入性别"<<endl;
    cin>>this->m_Sex;
    time (&timep);
    p=gmtime(&timep);
    m_Hour=p->tm_hour;
    m_Mine=p->tm_min;
    m_Sec=p->tm_sec;

}

class Teacher:public People//继承和组合同用
{
    string m_TeacheSb;
    string m_TeacheGa;
   
    public:
        virtual void RegistMessage();
        virtual void ShowMessage();//类外定义形式
        string OutName();
        Teacher(string name1="-",int age1=0,string sex1="_",string m_TeacherSb="_",string m_TeacheGa="_");
        ~Teacher(){};

};
Teacher::Teacher(string name1,int age1,string sex1,string m_TeacherSb1,string m_TeacheGa1):People(name1,age1,sex1)//注意在声明时不用写默认值
{
    m_TeacheSb=m_TeacherSb1;
    m_TeacheGa=m_TeacheGa1;
}
string Teacher::OutName()
{
    return m_Name;
}
void Teacher::ShowMessage()
{
    cout<<"姓名\t年龄\t性别\t所授科目\t所授年级"<<endl;
    cout<<m_Name<<"\t"<<m_Age<<"\t"<<m_Sex<<"\t"<<m_TeacheSb<<"\t"<<m_TeacheGa<<endl;

}
void Teacher::RegistMessage()
{
    puts("姓名:");
    cin>>m_Name;
    puts("年龄:");
    cin>>m_Age;
    puts("性别:");
    cin>>m_Sex;
    puts("请输入所授科目");
    cin>>m_TeacheSb;
    puts("请输入所授班级");
    cin>>m_TeacheGa;
    time (&timep);
    p=gmtime(&timep);
    m_Hour=p->tm_hour;
    m_Mine=p->tm_min;
    m_Sec=p->tm_sec;

}
class Student:public People
{
    int m_Number;
public:
    void RegistMessage();
    void ShowMessage();
    int OutNumber();
    Student(string name="_",int age=0,string sex="_",int number=0):People(name,age,sex){m_Number=number;}
    ~Student(){};
};
void Student::ShowMessage()
{
        cout<<"学号\t姓名\t性别\t年龄"<<endl;
        cout<<m_Number<<"\t"<<m_Sex<<"\t"<<m_Name<<"\t"<<m_Age<<endl;

}
void Student::RegistMessage()
{

    puts("姓名:");
    cin>>m_Name;
    puts("年龄:");
    cin>>m_Age;
    puts("性别:");
    cin>>m_Sex;
    puts("请输入学号");
    cin>>m_Number;
    time (&timep);
        p=gmtime(&timep);
    m_Hour=p->tm_hour;
    m_Mine=p->tm_min;
    m_Sec=p->tm_sec;
   
}
int Student::OutNumber()
{
    return m_Number;
}
void WantThing(People* pe)//测试多态性
{
   
    pe->ShowMessage();
}

void main()
{

    int i;
    int j;
    FILE *fp_Student;
    FILE *fp_Teacher;
    Teacher te[2];
    Student st[2];
    int choose1;
    int choose2;
    cout<<"。。。。。。。。。。。。。。。。。欢迎使用签到系统。。。。。。。。。。。。。。。\n\n"<<endl;
    cout<<"(1)查询\t\t(2学生入口\t(3)教师入口\t(4)退出"<<endl;
    cin>>choose1;
    switch(choose1)
    {
        case 1:
            puts("1 学生\t 2 教师");            
                    cin>>choose2;
                    switch(choose2)
                    {
                    case 1:
                            if((fp_Student=fopen("stu.txt","r"))==NULL)
                          {
                                cout<<"文件打开失败"<<endl;
                                 exit(0);
                        }
                          else
                          {
                                for(i=0;i<2;i++)
                                    {
                                     fread(&st[i],sizeof(Student),1,fp_Student);
                                    }
                          }
                    fclose(fp_Student);  
                    cout<<"签到情况:"<<endl;
                    for(i=0;i<2;i++)
                        
                    st[i].ShowMessage();
                    break;


                     case 2:       if((fp_Teacher=fopen("Teacher.dat","rb"))==NULL)
                              {
                                    cout<<"文件打开失败"<<endl;
                                     exit(0);
                            }
                              else
                              {
                                    for(j=0;j<2;j++)
                                        {
                                         fread(&st,sizeof(Teacher),1,fp_Teacher);
                                        }
                              }
                    fclose(fp_Teacher);  
                    cout<<"签到情况:"<<endl;
                    for(j=0;j<2;j++)
                        te[j].ShowMessage();
                    //    st.ShowMessage();
                     break;
                    }
                    break;
        case 2:
                 if((fp_Student=fopen("stu.txt","w"))==NULL)
                       {
                            cout<<"打开文件失败"<<endl;
                           
                       }
                       else
                       {
                            for(i=0;i<2;i++)
                            {
                                st[i].RegistMessage();
                                fwrite(&st,sizeof(Student),1,fp_Student);
                                cout<<"恭喜你成功签到!!!"<<endl;
                            }
                           
                            fclose(fp_Student);
                            cout<<"文件关闭!!"<<endl;
                       }
                       for(i=0;i<2;i++)
                       {
                           st[i].ShowMessage();
                           st[i].operator <<(cout);
                       }
                       break;

       case 3:
            if((fp_Teacher=fopen("Teacher.dat","wb"))==NULL)
                       {
                            cout<<"打开文件失败"<<endl;
                           
                       }
                       else
                       {
                            for(j=0;j<2;j++)
                            {
                                te[j].RegistMessage();
                                fwrite(&te[j],sizeof(Teacher),1,fp_Teacher);
                                cout<<"恭喜你成功签到!!!"<<endl;
                            }
                           
                            fclose(fp_Teacher);
                            cout<<"文件关闭!!"<<endl;
                       }
                       
            break;
        case 4:exit(0);break;
        }

    }
搜索更多相关主题的帖子: age include public 
2012-12-10 13:13
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:10 
我运行了 没提示错误啊

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-12-10 15:53
芦浩轩
Rank: 1
来 自:河北
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-25
收藏
得分:0 
回复 2楼 mmmmmmmmmmmm
读取操作。。
2012-12-10 16:02
mmmmmmmmmmmm
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:8
帖 子:388
专家分:1809
注 册:2012-11-2
收藏
得分:0 
能把错误截图发上来么

我们的目标只有一个:消灭0回复!
while(1)
++money;
2012-12-10 16:10
zmxx
Rank: 2
等 级:论坛游民
帖 子:3
专家分:15
注 册:2012-12-10
收藏
得分:10 
图片附件: 游客没有浏览图片的权限,请 登录注册

这是怎么回事啊?求指点
2012-12-10 20:47
zmxx
Rank: 2
等 级:论坛游民
帖 子:3
专家分:15
注 册:2012-12-10
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册

这是怎么回事啊?求指点
2012-12-10 20:47
芦浩轩
Rank: 1
来 自:河北
等 级:新手上路
帖 子:11
专家分:0
注 册:2012-3-25
收藏
得分:0 
回复 4楼 mmmmmmmmmmmm
写进去以后再此运行程序。。读取文件里的内容就会出现以下错误
图片附件: 游客没有浏览图片的权限,请 登录注册
2012-12-10 22:42
快速回复:各位!!看看好奇怪为什么写进去的文件读取时老出错,当吧数据类型由s ...
数据加载中...
 
   



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

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