如何在c++中使用二进制文件in/out读/写结构体的字符串类型成员?

66bbxpm5  于 2023-02-01  发布在  其他
关注(0)|答案(3)|浏览(168)

我有2个C++代码:一个用于将数据写入二进制文件,另一个用于读取该文件。
write.cpp代码如下:

#include <iostream>
#include <fstream>
using namespace std;
const int NAME_SIZE = 51;
struct Data
{
    char name[NAME_SIZE];
    int age;
};

int main()
{
    Data person;
    char again;

    fstream people("people.db", ios::out | ios::binary);

    do
    {
        cout << "Enter the following data about a "<< "person:\n";
        cout << "Name: ";
        cin.getline(person.name, NAME_SIZE);
        cout << "Age: ";
        cin >> person.age;
        cin.ignore();

        people.write(reinterpret_cast<char *>(&person),sizeof(person));

        cout << "Do you want to enter another record? ";
        cin >> again;
        cin.ignore();
    } while (again == 'Y' || again == 'y');
    people.close();


    return 0;
}

read.cpp代码如下:

#include <iostream>
#include <fstream>
using namespace std;
const int NAME_SIZE = 51;
struct Data
{
    char name[NAME_SIZE];
    int age;
};

int main()
{
    Data person;
    char again;
    fstream people;

    people.open("people.db", ios::in | ios::binary);

    if (!people)
    {
        cout << "Error opening file. Program aborting.\n";
        return 0;
    }

    cout << "Here are the people in the file:\n\n";
    people.read(reinterpret_cast<char *>(&person),sizeof(person));

    while (!people.eof())
    {
        cout << "Name: ";
        cout << person.name << endl;
        cout << "Age: ";
        cout << person.age << endl;

        cout << "\nPress the Enter key to see the next record.\n";
        cin.get(again);
        people.read(reinterpret_cast<char *>(&person),sizeof(person));
    }
    cout << "That's all the data in the file!\n";
    people.close();
    return 0;
}

上面提到的代码工作正常。当我在结构中使用字符串类型成员时出现了问题:
新的write.cpp

#include <iostream>
#include <fstream>
using namespace std;

struct Data
{
    string name;
    int age;
};

int main()
{
    Data person;
    char again;

    fstream people("people.db", ios::out | ios::binary);

    do
    {
        cout << "Enter the following data about a "<< "person:\n";
        cout << "Name: ";
        cin>>person.name;
        cout << "Age: ";
        cin >> person.age;
        cin.ignore();

        people.write(reinterpret_cast<char *>(&person),sizeof(person));

        cout << "Do you want to enter another record? ";
        cin >> again;
        cin.ignore();
    } while (again == 'Y' || again == 'y');
    people.close();


    return 0;
}

新的read.cpp

#include <iostream>
#include <fstream>
using namespace std;

struct Data
{
    string name;
    int age;
};

int main()
{
    Data person;
    char again;
    fstream people;

    people.open("people.db", ios::in | ios::binary);

    if (!people)
    {
        cout << "Error opening file. Program aborting.\n";
        return 0;
    }

    cout << "Here are the people in the file:\n\n";
    people.read(reinterpret_cast<char *>(&person),sizeof(person));

    while (!people.eof())
    {
        cout << "Name: ";
        cout << person.name << endl;
        cout << "Age: ";
        cout << person.age << endl;

        cout << "\nPress the Enter key to see the next record.\n";
        cin.get(again);
        people.read(reinterpret_cast<char *>(&person),sizeof(person));
     }
     cout << "That's all the data in the file!\n";
     people.close();
    return 0;
}

现在我运行read.cpp的时候,程序读不到string,程序崩溃了,我必须用string作为结构的成员,这个问题怎么解决?

brccelvz

brccelvz1#

想到的唯一方法是单独写入以下数据:
1.字符串的长度。
1.字符串的字符数组。
1.年龄。
并分别阅读它们。
创建写/读Data示例的函数,使它们知道彼此的实现策略。

std::ostream& write(std::ostream& out, Data const& data)
{
   size_t len = data.name.size();
   out.write(reinterpret_cast<char const*>(&len), sizeof(len));
   out.write(data.name.c_str(), len);
   out.write(reinterpret_cast<char const*>(&data.age));
   return out;
}

std::istream& read(std::istream& in, Data& data)
{
   size_t len;
   in.read(reinterpret_cast<char*>(&len), sizeof(len));

   char* name = new char[len+1];
   in.read(name, len);
   name[len] = '\0';
   data.name = name;
   delete [] name;

   in.read(reinterpret_cast<char*>(&data.age));
   return in;
}

并像第一种方法一样使用它们。
而不是使用

people.write(reinterpret_cast<char *>(&person),sizeof(person));

使用

write(people, person);

而不是使用

people.read(reinterpret_cast<char *>(&person),sizeof(person));

使用

read(people, person);
e3bfsja2

e3bfsja22#

一个问题是sizeof(person.Name)并没有给予你所认为的那样。它总是给出相同的大小(在我的例子中是28字节),不管你给你的人分配什么字符。名称字符串。这是因为std::string至少包含:

  • 指向实际字符串的指针
  • 保存可用大小和已用大小的其他数据结构

因此,不能调用people.write(reinterpret_cast<char *>(&person),sizeof(person));。字符串的内容不在&person(它位于std::string中指针指向的任何地方)
那么,当你从文件中阅读cout << person.name << endl;后会发生什么呢?当你把person写入people.db时,你实际上读取了person.name的字符串指针所指向的地址(而不是内容)。当然,这不是一个有效的内存位置,当你从文件中读取它后,再次读取它。

lqfhib0f

lqfhib0f3#

下面的代码片段对您的情况可能会有帮助。可以使用分隔符和预定义的字符串长度,而不是写入字符串的长度。

constexpr char delimiter = '\0';
constexpr uint32_t maxStringSize = 1024;

struct Data
{
    string name;
    int age;
};

写入文件时,在字符串后放置一个delimiter
假设我们有一个Data structure {"John", 42},那么我们可以写如下:

std::ofstream outStream(filename, std::ios::binary);
outStream << structure.name << delimiter << structure.age;
outStream.close();

阅读文件并不是写入的镜像(不幸的是)。
我们将使用std::ifstream::getline来读取字符串,而不知道它的大小。(省略错误检查)

std::ifstream istrm(filename, std::ios::binary);
Data dataRead;

// string input - use a buffer and look for the next delimiter
char* buf = new char[maxStringSize];
istrm.getline(buf, maxStringSize, delimiter);
dataRead.name = std::string(buf);

// the number input
istrm >> dataRead.age;

对于如何读/写这个struct的向量的灵感,你可以查看我的repository

相关问题