二进制读/写运行时失败
我研究过 C++ 中的二进制读写对象,但遇到了一些问题。它“有效”,但此外我还得到了大量的错误/“信息”输出。
我所做的是
Person p2;
std::fstream file;
file.open( filename.c_str(), std::ios::in | std::ios::out | std::ios::binary );
file.seekg(0, std::ios::beg );
file.read ( (char*)&p2, sizeof(p2));
file.close();
std::cout << "Name: " << p2.name;
Person 是一个包含 string name
和 intage
的简单结构。当我运行该程序时,它输出“名称:Bob”,因为我已经编写了一个写入文件的程序(因此该对象已经在文件名中)。
除了输出名称之外,它还输出:
* glibc detectors * program: double free og Corruption (fastttop): ***
Backtrace:
...
Memory map:
...
Abort
I've looked at binary reading and writing objects in c++ but are having some problems. It "works" but in addition I get a huge output of errors/"info".
What I've done is
Person p2;
std::fstream file;
file.open( filename.c_str(), std::ios::in | std::ios::out | std::ios::binary );
file.seekg(0, std::ios::beg );
file.read ( (char*)&p2, sizeof(p2));
file.close();
std::cout << "Name: " << p2.name;
Person is a simple struct containing string name
and int age
. When I run the program it outputs "Name: Bob" since I have already made a program to write to a file (so the object is already in filename).
IN ADDITION to outputting the name it also outputs:
* glibc detected * program: double free og corruption (fastttop): ***
Backtrace:
...
Memory map:
...
Abort
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Person 结构体中的名称字符串是字符数组还是 STL 字符串?您无法通过在其之上读取二进制数据来填充 STL 字符串,因为数据格式不可序列化(包含指针)
Is the name string in the Person struct a character array or a STL string? You can't fill in an STL String by binary reading data over top of it, since the data format is not serializable (contains pointers)
了解如何将信息写入文件以及如何构建
Person
结构也会很有趣。如果文件是纯文本没有任何问题,我的建议是使用
string::c_str()
(返回 const char*)以及使用 < code>itoa() 或itoa_s()
获取 char* 形式的整数。您还可以在 Person 中拥有一个或多个构造函数:
然后,当您从文件中提取数据时,只需使用该数据调用构造函数即可。
It would be interesting to see how you write the information to file as well, as well as how the
Person
struct is built.If you don't have any problem that the file is plain text, my suggestion would be to write to file using
string::c_str()
(which returns a const char*) as well as usingitoa()
oritoa_s()
to get the integer as a char*.You can also have one or several constructors in Person:
then, when you extract the data from the file you just call the constructor with that data.
p2.name 是一个 char* 并且您正在写入和读取指针值,而不是它所指向的值。或者 p2.name 是更复杂的类型,例如 std::string ,它使用具有相同问题的内部指针。
序列化类通常需要做更多的工作,而不仅仅是转储内存表示。
Either p2.name is a char* and you are writing and reading the pointer value, not what is pointed by it. Or p2.name is a more complex type such as std::string which is using internaly pointers with the same problem.
Serializing classes often need more work than just dumping the memory representation.
您说您将 Person 对象写入了文件。您是否尝试使用转储工具来查看文件中的内容是否是您所期望的?
您还尝试过使用普通字符而不是使用字符串(正如@bdk指出的那样)吗?
You said you wrote the Person object to a file. Did you tried to use a dump tool to see if what you have inside the file is what you are expecting?
Also did you tried to instead of using string, use a ordinary char (as @bdk pointed out) ?
当使用二进制IO时,大小必须是固定的。如果在这里使用 STL 字符串,则会出现问题,因为 STL 字符串的大小是任意的。
When you use binary IO, the size must be fixed. If you use STL string here, it would have a problem as the size of a STL string is arbitrary.