C++ 如何使用cpp将类的成员写入二进制文件?

C++ 如何使用cpp将类的成员写入二进制文件?,c++,class,binaryfiles,data-members,C++,Class,Binaryfiles,Data Members,我正在用cpp编写一个小型后端,在这里我必须将一些纵横字谜数据写入一个二进制文件。我创建了一个XWPuzzle类,它的所有数据成员都是公共的,xWord是这个类的一个对象。下面是我试图写入二进制文件的代码片段: #include"binIncludes.h" int main(int argc, char** argv) { cout<<"Bin read-write Tester\n"; xWord.title="Yeah!\0"; xWord.autho

我正在用cpp编写一个小型后端,在这里我必须将一些纵横字谜数据写入一个二进制文件。我创建了一个XWPuzzle类,它的所有数据成员都是公共的,xWord是这个类的一个对象。下面是我试图写入二进制文件的代码片段:

#include"binIncludes.h"

int main(int argc, char** argv)
{
    cout<<"Bin read-write Tester\n";
    xWord.title="Yeah!\0";
    xWord.author="Nobody\0";
    xWord.grid=147;
    xWord.userGrid=109;
    xWord.clues[0]="as\0";
    xWord.clues[1]="de\0";
    xWord.clues[2]="re\0";
    xWord.solution[0]="ASSET\0";
    xWord.solution[1]="DEER\0";
    xWord.solution[2]="REED\0";
    xWord.checksum=(int)(xWord.title.at(0))+(int)(xWord.author.at(0))+xWord.grid+xWord.userGrid;
    xWord.checksum+=(int)(xWord.clues[0].at(0))+(int)(xWord.clues[1].at(0))+(int)(xWord.clues[2].at(0));
    xWord.checksum+=(int)(xWord.solution[0].at(0))+(int)(xWord.solution[1].at(0))+(int)(xWord.solution[2].at(0));
    fstream file;
    file.open("binTest.bin",ios::out|ios::binary);
    file.write(SIGNATURE,sizeof(SIGNATURE));
    file.write(VERSION,sizeof(VERSION));
    file.write(TYPE,sizeof(TYPE));
    file.write((char*)xWord.checksum,sizeof(xWord.checksum));
    file.write(xWord.title.c_str(),xWord.title.size());
    file.write(xWord.author.c_str(),xWord.author.size());
    file.write((char*)xWord.grid,sizeof(xWord.grid));
    file.write((char*)xWord.userGrid,sizeof(xWord.userGrid));
    file.close();
    cout<<"File written\n";
    _getch();
    return 0;
}
调试器在MSVCR100.dll处引发第一次机会异常(访问冲突读取位置错误)。我是否以错误的方式使用了某个类的成员而做了错误的事情?这是一个独立的项目,没有任何家庭作业。我已经为此奔波了两天了。任何具体的帮助都将不胜感激。谢谢

除了这门课上的资料外,我还有其他资料要写

编辑:

XWPuzzle.h:

#ifndef XWPULZZLE_H
#define XWPUZZLE_H

#include"binIncludes.h"

class XWPuzzle
{
    public:
        string title;
        string author;
        int grid;
        int userGrid;
        string clues[3];
        string solution[3];
        int checksum;
} xWord;

#endif
binDefines.h:

#ifndef BIN_DEFINES_H
#define BIN_DEFINES_H

#include"binIncludes.h"

#define SIGNATURE "BinFile\0"
#define VERSION "0.1.1\0"
#define TYPE "With Solution\0"

#endif
B.h包括:

#ifndef BIN_INCLUDES_H
#define BIN_INCLUDES_H

#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>

using namespace std;

#include"binDefines.h"
#include"XWPuzzle.h"

#endif
\ifndef BIN\u包括
#定义BIN_包括
#包括
#包括
#包括
#包括
使用名称空间std;
#包括“binDefines.h”
#包括“XWPuzzle.h”
#恩迪夫

您正在将整数强制转换为指针,并尝试使用该指针写入数据。那是错误的。您应该获取指向校验和的指针,强制转换它,然后写入

write((char*)(&xWord.checksum),sizeof(xWord.checksum))


与grid和userGrid字段相同

您正在将整数强制转换为指针,并尝试使用该指针写入数据。那是错误的。您应该获取指向校验和的指针,强制转换它,然后写入

write((char*)(&xWord.checksum),sizeof(xWord.checksum))


与grid和userGrid字段相同

我们至少需要查看
XWPuzzle
的声明。xWord对象是在哪里生成的?我想看看你的班级申报单,抱歉耽搁了。我已经添加了XWPuzzle的声明。除非它们是固定大小的字符串,否则您将获得的乐趣就是读回它们。一个简单的处理方法是先写长度,然后写字符串数据,这样你就知道要读多少了。这不就是为什么它们是以NUL结尾的字符串吗?我想这就是使用nul结尾字符串的原因。我们至少需要看看
XWPuzzle
的声明。xWord对象是在哪里生成的?我想看看你的班级申报单,抱歉耽搁了。我已经添加了XWPuzzle的声明。除非它们是固定大小的字符串,否则您将获得的乐趣就是读回它们。一个简单的处理方法是先写长度,然后写字符串数据,这样你就知道要读多少了。这不就是为什么它们是以NUL结尾的字符串吗?我想这就是使用nul终止字符串的原因。非常感谢!新手搞错了,非常感谢!新手的错误。
#ifndef BIN_INCLUDES_H
#define BIN_INCLUDES_H

#include<iostream>
#include<conio.h>
#include<string>
#include<fstream>

using namespace std;

#include"binDefines.h"
#include"XWPuzzle.h"

#endif