c++ - 用C++写入二进制文件

标签 c++

<分区>

我为学生创建了一个类(class)。这是类(class)。

class student
{
  int rollno,marks;
  char name[20];


 public:
    void show();
    void get();
    int filecreate(const string &,fstream &);
    int fileopen(const string &,fstream &);
    static int add(const student &,fstream &);
    static int read(const student &,fstream &,int);
    static int update(const student &,fstream &,int);
    int showall(fstream &);
    int view(fstream &,int );
    int adddummy(fstream &);
};

这是我在二进制文件中写入记录的添加方法。

int student::add(const student &s,fstream &fp)
{
        fp.seekp(0, ios::end);
        if(fp.write((char*)&s,sizeof s))
            return 1;
        return 0;
}

如果我写入对象 s 的值,写入文件的值是什么。是否只有 rollno、marks 和 name(仅限数据成员?)。现在我想为 fstream 添加一个成员。添加成员后,如果我写入文件,将写入文件的数据是什么(fstream 值是否也会写入文件?)

最佳答案

我认为您首先要实现的是类实例的序列化

This问题及其答案可能有助于此目的。 特别是这个link (在评分最高的答案中提供)关于 C++ 序列化的常见问题解答可能值得您考虑。

关于c++ - 用C++写入二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7976939/

相关文章:

c++ - 如何删除依赖项目中相同依赖源的重复项

c++ - 在 QStringList 中搜索特定项目,然后搜索可能包含项目的其他项目

c++ - 如何从包含 csv 数据的字符串创建字符串数组?

C++ 指针和内存泄漏

c++ - 该公式在 "accumulate"中采用哪些参数?

c++ - 看不懂迭代器的问题?

c# - 将数据从 C++ 传递到 C#

c++ - 编译错误

c++ - Memset 一个对象

C++ 继承的模板类无权访问基类