Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.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++;将std::string结构数组读写到二进制文件中 我对C++非常陌生(问题是我的家庭作业的一部分)。< /P>_C++_Serialization_Struct_Fstream - Fatal编程技术网

c++;将std::string结构数组读写到二进制文件中 我对C++非常陌生(问题是我的家庭作业的一部分)。< /P>

c++;将std::string结构数组读写到二进制文件中 我对C++非常陌生(问题是我的家庭作业的一部分)。< /P>,c++,serialization,struct,fstream,C++,Serialization,Struct,Fstream,我需要在二进制文件中存储一个结构数组并将其读回。问题是我的结构包含std::string字段。在另一边,我能读懂字符串,但后面什么也看不懂。以下是我的结构定义: struct Organization { int id; string name; float paidTaxes; }; 下面是我将此结构的数组写入文件的代码: void writeToFile(Organization *orgs, int n) { ofstream file("org

我需要在二进制文件中存储一个结构数组并将其读回。问题是我的结构包含
std::string
字段。在另一边,我能读懂字符串,但后面什么也看不懂。以下是我的结构定义:

struct Organization {
    int id;
    string name;
    float paidTaxes;
};
下面是我将此结构的数组写入文件的代码:

void writeToFile(Organization *orgs, int n) {
    ofstream file("orgs.bin", ios::out | ios::binary);
    if (!file) { return; }
    for (int i = 0; i < n; i++) {
        // determine the size of the string
        string::size_type sz = orgs[i].name.size();
        file.write(reinterpret_cast<char*>(&orgs[i].id), sizeof(int));
        // write string size
        file.write(reinterpret_cast<char*>(&sz), sizeof(string::size_type));
        // and actual string
        file.write(orgs[i].name.data(), sizeof(sz));
        file.write(reinterpret_cast<char*>(&orgs[i].paidTaxes), sizeof(float));
    }
    file.close();
}
void writeToFile(组织*orgs,int n){
流文件(“orgs.bin”,ios::out | ios::binary);
如果(!file){return;}
对于(int i=0;i
这里是代码部分,我在这里读回这个文件:

count = 0;
Organization org;
ifstream file;
file.open("orgs.bin", ios::binary);
while (file.good()) {
    string::size_type sz;
    file.read(reinterpret_cast<char*>(&org.id), sizeof(int));
    file.read(reinterpret_cast<char*>(&sz), sizeof(string::size_type));
    org.name.resize(sz);
    file.read(&org.name[0], sz);
    file.read(reinterpret_cast<char*>(&org.paidTaxes), sizeof(float));
    count++;
}
count=0;
组织机构;
ifstream文件;
打开(“orgs.bin”,ios::binary);
while(file.good()){
字符串::size_类型sz;
read(reinterpret_cast(&org.id),sizeof(int));
read(reinterpret_cast(&sz),sizeof(string::size_type));
org.name.resize(sz);
file.read(&org.name[0],sz);
read(reinterpret_cast(&org.paidTaxes),sizeof(float));
计数++;
}
据我所知,我需要运行这个循环来确定一个文件中存储了多少个结构。当我运行调试器时,我成功地读取了
id
字段、字符串大小和实际字符串。但是,我从未得到正确的
paidTaxes
字段(类型为
float
),后续循环返回垃圾


提前谢谢

问题出在这一行

file.write(orgs[i].name.data(), sizeof(sz));
这是一个打字错误,而不是用法

file.write(orgs[i].name.data(), sz);
您应该写入sz字节,但不能写入sizeof(sz)。您的代码还有一个问题。您将读取最后一条记录两次,因为file.good()将在最后一条记录之后返回true。你可以这样读:

    while (file.good()) {
    string::size_type sz;
    if( !file.read(reinterpret_cast<char*>(&org.id), sizeof(int)) )
        break;
    file.read(reinterpret_cast<char*>(&sz), sizeof(string::size_type));
    org.name.resize(sz);
    file.read(&org.name[0], sz);
    file.read(reinterpret_cast<char*>(&org.paidTaxes), sizeof(float));
    count++;

    std::cout << org.id << " " << org.name << " " << org.paidTaxes << std::endl;
}
while(file.good()){
字符串::size_类型sz;
if(!file.read(reinterpret_cast(&org.id),sizeof(int)))
打破
read(reinterpret_cast(&sz),sizeof(string::size_type));
org.name.resize(sz);
file.read(&org.name[0],sz);
read(reinterpret_cast(&org.paidTaxes),sizeof(float));
计数++;

std::cout Thank!还有一个问题:在开始时写入存储的数组大小以避免此循环是否合理?我将读取数组大小,创建具有正确大小的结构数组,并使用单个循环从文件中填充数据?是的,您可以这样做,这非常简单且可靠