c ++使用std :: string读取和写入struct数组到二进制文件中

时间:2021-10-09 11:08:11

I'm very new to c++ (and the question is a part of my homework).

我对c ++很新(问题是我作业的一部分)。

I need to store an array of structs in a binary file and read them back. The problem is that my struct contains std::string field. On the other side I can read the string, but nothing after it. Here is my structure definition:

我需要在二进制文件中存储一个结构数组并将其读回。问题是我的struct包含std :: string字段。另一方面,我可以读取字符串,但没有任何内容。这是我的结构定义:

struct Organization {
int id;
string name;
float paidTaxes;
};

and here is the code where I write array of this struct to a file:

这是我将此结构的数组写入文件的代码:

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();

}

and here is the code part where I'm reading this file back:

这是我正在阅读此文件的代码部分:

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

As far as I understand, I need to run this loop to determine how many structs are stored in a file. When I run debugger, I successfully read id field, string size and actual string. However, I never get proper paidTaxes field (type of float) and subsequent loops return me junk.

据我所知,我需要运行此循环来确定文件中存储的结构数量。当我运行调试器时,我成功读取了id字段,字符串大小和实际字符串。但是,我从来没有得到适当的paidTaxes字段(浮点类型),后续循环返回垃圾。

thanks in advance!

提前致谢!

1 个解决方案

#1


The problem is in this line

问题出在这一行

file.write(orgs[i].name.data(), sizeof(sz));

Here is a typo, insted use

这是一个错字,插入使用

file.write(orgs[i].name.data(), sz);

You shoud write sz bytes, but not sizeof(sz). There is also one more problem with your code. You will read the last record twice, because file.good() will return true after the last record. You can read like that:

你应该写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;
}

#1


The problem is in this line

问题出在这一行

file.write(orgs[i].name.data(), sizeof(sz));

Here is a typo, insted use

这是一个错字,插入使用

file.write(orgs[i].name.data(), sz);

You shoud write sz bytes, but not sizeof(sz). There is also one more problem with your code. You will read the last record twice, because file.good() will return true after the last record. You can read like that:

你应该写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;
}