u013429409
chenerzhu
采纳率50%
2015-03-17 14:05 阅读 2.6k

关于C++文件的读写read和write问题!

第一个:这样写读出结果会对,但是有时候也会出错。换成中文的有时也会出错。
#include
#include
#include

using namespace std;

class People
{
private:
struct
{
string name;
string weight;
string tall;
string sex;
}people;
public:
People(){}
People(string name, string weight, string tall, string sex)
{
people.name=name;
people.weight=weight;
people.tall=tall;
people.sex=sex;
}
string get_Name()
{
return people.name;
}
string get_Sex()
{
return people.sex;
}
};

int main()
{
fstream file;
file.open("people.txt",ios::out|ios::binary);
People pe("aaa","bbb","ccc","sda");
People pe2;
file.write(reinterpret_cast(&pe),sizeof(pe));
file.close();
file.open("people.txt",ios::in|ios::binary);
file.read(reinterpret_cast(&pe2),sizeof(pe2));
cout<<pe2.get_Sex()<<" "<<pe2.get_Name();
file.close();
return 0;
}

第二种:是第一种写的文件读出来,为什么就会出错?
#include
#include
#include

using namespace std;

class People
{
private:
struct
{
string name;
string weight;
string tall;
string sex;
}people;
public:
People(){}
People(string name, string weight, string tall, string sex)
{
people.name=name;
people.weight=weight;
people.tall=tall;
people.sex=sex;
}
string get_Name()
{
return people.name;
}
string get_Sex()
{
return people.sex;
}
};

int main()
{
fstream file;
People pe2;
file.open("people.txt",ios::in|ios::binary);
file.read(reinterpret_cast(&pe2),sizeof(pe2));
cout<<pe2.get_Sex()<<" "<<pe2.get_Name();
file.close();
return 0;
}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • oyljerry oyljerry 2015-03-17 16:30

    string是动态类型,你sizeof方式求大小会有问题,造成数据读取不对。
    结构体要用基本数据类型。

    点赞 评论 复制链接分享
  • u013429409 chenerzhu 2015-03-18 01:55

    能解释下为什么吗?还有我现在改成基本类型了。这个例子可以了。我还有另外一个例子,可以写,但是不可以读。能帮我看下吗?谢谢!
    #include
    #include
    #include
    #include

    using namespace std;

    const int NUM=1000;
    static int employeerNum=0;
    static int loadNum=0;

    class Employeer
    {
    private:
    struct
    {
    char name[30];
    char gender[10];
    char dateofbirth[30];
    char category[30];
    }employeerInfor;
    public:
    Employeer(char name[],char gender[],char dateofbirth[],char category[])
    {
    strcpy(employeerInfor.name,name);
    strcpy(employeerInfor.gender,gender);
    strcpy(employeerInfor.dateofbirth,dateofbirth);
    strcpy(employeerInfor.category,category);
    }//构造函数
    char *Get_name()
    {
    return employeerInfor.name;
    }
    char *Get_gender()
    {
    return employeerInfor.gender;
    }
    char *Get_dateofbirth()
    {
    return employeerInfor.dateofbirth;
    }
    char *Get_category()
    {
    return employeerInfor.category;
    }
    virtual float computeSalary()=0;//纯虚函数
    friend void Save(int i);
    friend int ReadDate();
    };

    Employeer *employeer[NUM];

    int ReadDate()
    {
    fstream outFile;
    outFile.open("Manager.txt",ios::in|ios::binary);
    if(outFile.fail())
    {
    return 0;
    }
    else
    {
    int i=loadNum;
    while(!outFile.eof())
    {
    outFile.read((char *)&employeer[i]->employeerInfor,sizeof(employeer[i]->employeerInfor));
    i++;
    }
    cout<<loadNum;
    loadNum=i-1;
    employeerNum=loadNum;//加载完成,总数变化
    }
    outFile.close();
    }
    int main()
    {
    ReadDate();
    return 0;
    }

    点赞 评论 复制链接分享

相关推荐