简体   繁体   English

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

[英]How to write members of a class to a binary file using cpp?

I'm writing a small back-end in cpp where I have to write some crossword data into a binary file. 我正在cpp中编写一个小型后端,在其中必须将一些填字游戏数据写入二进制文件。 I have created an XWPuzzle class with all its data members public, and xWord is an object of this class. 我创建了一个XWPuzzle类,其中所有数据成员都是公共的,并且xWord是此类的对象。 Here is the piece of code with which I'm trying to write to the binary file: 这是我要写入二进制文件的代码:

#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;
}

SIGNATURE, VERSION and TYPE are null-terminated strings, but the compiled program throws an error when it gets to the line SIGNATURE,VERSION和TYPE是以空字符结尾的字符串,但是编译后的程序到达该行时会引发错误

file.write((char*)xWord.checksum,sizeof(xWord.checksum));

The debugger throws a First-chance exception (Access Violation reading location error) at MSVCR100.dll. 调试器在MSVCR100.dll上引发First-chance异常(访问冲突读取位置错误)。 Am I doing something wrong by using a member of a class the wrong way? 我通过错误的方式使用班级成员来做错什么吗? This is an independent project and not any homework. 这是一个独立的项目,而不是任何作业。 I've been running around for this for two days now. 我已经为此花了两天时间。 Any specific help appreciated. 任何具体的帮助表示赞赏。 Thanks. 谢谢。

I have other data to be written also, in addition to those from this class. 除了来自此类的数据,我还有其他数据要写。

EDIT: 编辑:

XWPuzzle.h: 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: 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

binIncludes.h: binIncludes.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

You are casting integer to a pointer and trying to write data by this pointer. 您正在将整数转换为指针,并尝试通过该指针写入数据。 That's wrong. 错了 You shoud get pointer to a checksum, cast it and then write 您应该获取指向校验和的指针,对其进行强制转换,然后编写

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

Same with grid and userGrid fields 与grid和userGrid字段相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM