C++ - How to write and read a structure that contain an object ? (to write and read binary)(C++ - 如何编写和读取包含对象的结构?(写入和读取二进制))
问题描述
我正在尝试在文件中写入 C 结构(以二进制形式写入)并读取它以恢复它.我不知道这是否可能.这是我所拥有的:
I'm trying to write a C structure in a file (to write in binary) and read it to recover it. I don't know if it is possible. Here is what I have :
head.hh:
#include <iostream>
typedef struct s_test
{
char cmd[5];
std::string str;
}t_test;
main.cpp:
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "head.hh"
int main()
{
t_test test;
int fd = open("test", O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, 0666);
test.cmd[0] = 's';
test.cmd[1] = 'm';
test.cmd[2] = 's';
test.cmd[3] = 'g';
test.str = "hello world";
write(fd, &test, sizeof(t_test));
close(fd);
fd = open("test", O_APPEND | O_CREAT | O_WRONLY, 0666);
t_test test2;
read(fd, &test2, sizeof(t_test));
std::cout << test2.cmd << " " << test2.str << std::endl;
return (0);
}
在输出上我有类似的东西:➜
And on the output I have something like : Ȟ�
推荐答案
要读取的文件以只写方式打开.
The file to read from was being opened as write only.
实际的 std::string
对象不能这样写.实际对象通常包含几个指针,可能还有一个大小,但不包含实际的字符数据.需要序列化.
The actual std::string
object can't be written that way. The actual object generally contains a couple of pointers and perhaps a size but not the actual character data. It need to be serialized.
如果您要编写 C++,您应该考虑学习使用文件流,而不是您已经拥有的.
If you're going to be writing C++ you should consider learning to use file streams rather than what you've got here.
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <io.h>
#include <iostream>
#include <string>
#include <vector>
typedef struct s_test
{
char cmd[5];
std::string str;
}t_test;
void Write(int fd, struct s_test* test)
{
write(fd, test->cmd, sizeof(test->cmd));
unsigned int sz = test->str.size();
write(fd, &sz, sizeof(sz));
write(fd, test->str.c_str(), sz);
}
void Read(int fd, struct s_test* test)
{
read(fd, test->cmd, sizeof(test->cmd));
unsigned int sz;
read(fd, &sz, sizeof(sz));
std::vector<char> data(sz);
read(fd, &data[0], sz);
test->str.assign(data.begin(), data.end());
}
int main()
{
t_test test;
int fd = open("test", O_APPEND | O_CREAT | O_TRUNC | O_WRONLY, 0666);
test.cmd[0] = 's';
test.cmd[1] = 'm';
test.cmd[2] = 's';
test.cmd[3] = 'g';
test.cmd[4] = 0;
test.str = "hello world";
std::cout << "Before Write: " << test.cmd << " " << test.str << std::endl;
Write(fd, &test);
close(fd);
fd = open("test", O_RDONLY, 0666);
t_test test2;
Read(fd, &test2);
std::cout << "After Read: " << test2.cmd << " " << test2.str << std::endl;
close(fd);
return (0);
}
这篇关于C++ - 如何编写和读取包含对象的结构?(写入和读取二进制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ - 如何编写和读取包含对象的结构?(写入和读取二进制)
基础教程推荐
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17