我有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作为结构的成员,这个问题怎么解决?
3条答案
按热度按时间brccelvz1#
想到的唯一方法是单独写入以下数据:
1.字符串的长度。
1.字符串的字符数组。
1.年龄。
并分别阅读它们。
创建写/读
Data
示例的函数,使它们知道彼此的实现策略。并像第一种方法一样使用它们。
而不是使用
使用
而不是使用
使用
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
的字符串指针所指向的地址(而不是内容)。当然,这不是一个有效的内存位置,当你从文件中读取它后,再次读取它。lqfhib0f3#
下面的代码片段对您的情况可能会有帮助。可以使用分隔符和预定义的字符串长度,而不是写入字符串的长度。
写入文件时,在字符串后放置一个
delimiter
。假设我们有一个
Data structure {"John", 42}
,那么我们可以写如下:阅读文件并不是写入的镜像(不幸的是)。
我们将使用
std::ifstream::getline
来读取字符串,而不知道它的大小。(省略错误检查)对于如何读/写这个
struct
的向量的灵感,你可以查看我的repository。