Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Can';在Ubuntu中读取.dat或.bin文件;分段故障(堆芯倾倒)“;_C++_Ubuntu - Fatal编程技术网

C++ Can';在Ubuntu中读取.dat或.bin文件;分段故障(堆芯倾倒)“;

C++ Can';在Ubuntu中读取.dat或.bin文件;分段故障(堆芯倾倒)“;,c++,ubuntu,C++,Ubuntu,所以,我最近才开始使用Ubuntu。 我正在构建一个非常简单的程序,它可以让我存储一些数据,然后再从中读取 使用C++、FSUSTH,将一些对象存储在.DAT文件中。 // function to store void storeRecord(Record r){ fstream afile; afile.open("file.dat" , ios::out | ios::binary | ios::app); afile.write(reinterpret_cas

所以,我最近才开始使用Ubuntu。 我正在构建一个非常简单的程序,它可以让我存储一些数据,然后再从中读取

使用C++、FSUSTH,将一些对象存储在.DAT文件中。

// function to store
void storeRecord(Record r){
     fstream afile;
     afile.open("file.dat" , ios::out | ios::binary | ios::app);
     afile.write(reinterpret_cast <const char*> (&r), sizeof(r) );
     afile.close();
}
//要存储的函数
无效存储记录(记录r){
流文件;
打开(“file.dat”,ios::out | ios::binary | ios::app);
文件写入(重新解释类型(&r),大小(r));
afile.close();
}
但是当我尝试(调用query()函数)读取同一个文件时,我得到了“分段错误(内核转储)”

void查询(){
记录r;
流文件;
打开(“file.dat”,ios::in | ios::binary);
while(afile.read(reinterpret_cast&r),sizeof(r))){
//做点什么
}
afile.close();
}
这在windows上使用。为什么呢

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class Record{
public:
    // Constructors
    Record();
    Record(string accountID, string name, string deptID, string password,           int role);
    ~Record();

    // Assessors and Mutators
    string getAccountID();
    string getName();
    string getDeptID();
    string getPW();
    int getRole();

    void setAccountID(string accountID);
    void setName(string name);
    void setDeptID(string deptID);
    void setPW(string PW);
    void setRole(int role);

    // Other functions
    string toString();

private:
    string accountID;
    string name;
    string deptID;
    string password;
    int role;       // normal user, HR personal, admin
};
#包括
#包括
#包括
使用名称空间std;
课堂记录{
公众:
//建设者
记录();
记录(字符串accountID、字符串名称、字符串deptID、字符串密码、int角色);
~Record();
//评估员和变异员
字符串getAccountID();
字符串getName();
字符串getDeptID();
字符串getPW();
int getRole();
作废setAccountID(字符串accountID);
void setName(字符串名);
void setDeptID(字符串deptID);
无效设置PW(字符串PW);
void setRole(int role);
//其他职能
字符串toString();
私人:
字符串accountID;
字符串名;
字符串deptID;
字符串密码;
int role;//普通用户、人力资源人员、管理员
};

您的
记录包含
std::string
字段

一个
字符串
当然包含指针,并且可以包含一些带有虚拟函数的内部数据(因此有一个,这是一个隐藏指针);然后这些数据就有了指针。而
字符串
不是一个数组(但是
字符的固定数组
应该是一个字段,例如
字符名[48];
…)

因此,您的代码有足够的可用性,所以您很幸运它在Linux上运行(不幸的是它“似乎”在Windows上运行)。。。顺便说一下,当它明显地“运行”(例如在你的窗口上),文件格式保持“未定义”,并且你将不能再次读取由程序的旧版本编写的数据文件(或者甚至是由旧的或不同的编译器和C++标准库编译的相同源)。 Linux有;这也许可以解释为什么你有一个。顺便说一句,如果您在同一个数据库中写入和读取同一个数据文件(这对您来说不是特别有用),那么Linux可能不会崩溃

要了解更多,您必须深入了解实现细节,而且您不想这样做

显然,你需要实施一些机制。然后首先(详细地)指定您的(逐字节,“字大小”很重要,并且您的文件格式应该与它们“独立”),也许可以使用一些符号。然后实现序列化例程,从基本类型开始。或者使用一些序列化库。您的文件格式和代码应该是可移植的(例如,Raspberry Pi和Linux/PC上的相同程序应该能够交换数据文件)


顺便说一句,我更喜欢使用一些文本格式,可能是使用(或XML)。也可以考虑类似的图书馆。在您的例子中,您的
记录可以轻松映射到SQLite表中的行或JSON对象。也许你应该问你的老师是否允许你使用它们……

查询中没有定义r。什么是记录?如果不是豆荚,你就有你的虫子,有。在实际的程序中。这是我的错。OOPSW记录的类型是什么?它是否有一些或一些(甚至是直接或内部)指针?您的代码有错误,所以您很幸运它在Linux上崩溃(不幸的是它“似乎”在Windows上工作)…顺便说一句,我建议不要使用二进制数据文件。如果你需要它们,花上几周的时间来详细说明它们。喜欢使用and或文本格式,或者请编辑您的问题以改进它并给出一些建议。当前它不是一个(因为
记录
未定义)
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class Record{
public:
    // Constructors
    Record();
    Record(string accountID, string name, string deptID, string password,           int role);
    ~Record();

    // Assessors and Mutators
    string getAccountID();
    string getName();
    string getDeptID();
    string getPW();
    int getRole();

    void setAccountID(string accountID);
    void setName(string name);
    void setDeptID(string deptID);
    void setPW(string PW);
    void setRole(int role);

    // Other functions
    string toString();

private:
    string accountID;
    string name;
    string deptID;
    string password;
    int role;       // normal user, HR personal, admin
};