Pointers 在c+中读写带有二进制文件指针的类+; 我一直试图用FScript在C++中编写一些数据,大多数例子都是这样的: #include <fstream> class Person{ public: int age; char name[50]; } int main(){ Person joe; joe.age = 50; strncpy(joe.name, "Joe Jones"); fstream file("filename.dat", ios_base::binary); file.write((char*)joe, sizeof(joe)); file.close(); }

Pointers 在c+中读写带有二进制文件指针的类+; 我一直试图用FScript在C++中编写一些数据,大多数例子都是这样的: #include <fstream> class Person{ public: int age; char name[50]; } int main(){ Person joe; joe.age = 50; strncpy(joe.name, "Joe Jones"); fstream file("filename.dat", ios_base::binary); file.write((char*)joe, sizeof(joe)); file.close(); },pointers,file-io,Pointers,File Io,当我像以前那样写数据时 Person joe(10); /* Initialize rest of joe */ file.write((char*)joe, sizeof(joe)); 生成的文件有8个字节的数据,4个字节表示年龄,4个字节表示好友集数组的地址 如何开始写入存储在数组中的实际数据?当我的类有其他类作为成员时,我也遇到了这个问题,例如人有车或类似的东西。首先,向类中添加一个将执行文件I/O的方法,然后您可以这样调用它: Person joe(); Person sally();

当我像以前那样写数据时

Person joe(10);
/* Initialize rest of joe */
file.write((char*)joe, sizeof(joe));
生成的文件有8个字节的数据,4个字节表示
年龄
,4个字节表示
好友集
数组的地址


如何开始写入存储在数组中的实际数据?当我的类有其他类作为成员时,我也遇到了这个问题,例如
或类似的东西。

首先,向类中添加一个将执行文件I/O的方法,然后您可以这样调用它:

Person joe();
Person sally();

fstream file("filename.dat", ios_base::out | ios_base::binary);
joe.serialize(file, true);//writes itself to the file being passed in
sally.serialize(file, true); //write another class to file after joe
file.close();
然后,您可以读取相同的文件来填充类实例:

fstream file("filename.dat", ios_base::in | ios_base::binary);
joe.serialize(file, false); //reads from file and fills in info
sally.serialize(file, false); //reads from file too
file.close();
类中的方法如下所示:

Person::serialize(fstream &fs, bool bWrite)
{
    int ages_length;
    if (bWrite) {
        fs.write(&age, sizeof(age));
        ages_length = ...; //you need to know how long the friendsAges array is
        fs.write(&ages_length, sizeof(ages_length)); //write the length to file
        fs.write(&friendsAges[0], sizeof(int)*ages_length); //write the variable-sized array to file
        fs.write(&name[0], sizeof(char)*50); //write a string of length 50 to file
    }
    else {
        fs.read(&age, sizeof(age));
        fs.read(&ages_length, sizeof(ages_length)); //read length of array from file
        //TODO: you will need to malloc some space for *friendsAges here
        fs.read(&friendsAges[0], sizeof(int)*ages_length); //read-in the variable length array
        fs.read(&name[0], sizeof(char)*50); //this only works if string length is always fixed at 50
    }
}

您正在尝试的是所谓的“类序列化”,如果您搜索它,您将找到大量的示例来回答您的问题。我建议您使用
std::string
而不是
char[50]
Person::serialize(fstream &fs, bool bWrite)
{
    int ages_length;
    if (bWrite) {
        fs.write(&age, sizeof(age));
        ages_length = ...; //you need to know how long the friendsAges array is
        fs.write(&ages_length, sizeof(ages_length)); //write the length to file
        fs.write(&friendsAges[0], sizeof(int)*ages_length); //write the variable-sized array to file
        fs.write(&name[0], sizeof(char)*50); //write a string of length 50 to file
    }
    else {
        fs.read(&age, sizeof(age));
        fs.read(&ages_length, sizeof(ages_length)); //read length of array from file
        //TODO: you will need to malloc some space for *friendsAges here
        fs.read(&friendsAges[0], sizeof(int)*ages_length); //read-in the variable length array
        fs.read(&name[0], sizeof(char)*50); //this only works if string length is always fixed at 50
    }
}