如何在c++中写一个结构体到文件并读取文件
How to write a struct to file in C++ and read the file?
我有一个结构体,并使用write()将其写入文件。
struct PointFull {
double lat;
double lon;
};
PointFull item;
void* buffer = malloc(sizeof (item));
int fd = open("output", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
if (fd < 0) {
printf("Error opening filen");
return 1;
}
memcpy(buffer, &item, sizeof (item));
write(fd2, buffer, sizeof (item));
现在我有一个名为"output"然后我要读取文件来测试数据。
int fd2 = open("output", O_RDONLY, S_IWUSR | S_IRUSR);
if (fd2 < 0) {
printf("Error opening filen");
return 1;
}
void* bufferRead;
bufferRead = malloc(100);
read(fd2, bufferRead,100);
目前,我有bufferRead,但我不知道如何读取缓冲区插入数据结构?
-
你宁愿分配一个大小为
sizeof(PointFull)
的缓冲区。因为如果struct的大小被改变并且变得比你硬编码的大小大,那么你就会得到一个bug。 -
使用本地缓冲区,除非你真的需要使用动态内存。我假设在您的代码中并不真正需要分配。只是您可能很容易忘记释放内存,而当函数返回时缓冲区会自动删除。
int fd2 = open("output", O_RDONLY, S_IWUSR | S_IRUSR);
if (fd2 < 0) {
printf("Error opening filen");
return 1;
}
char bufferRead[sizeof(PointFull)];
read(fd2, bufferRead, sizeof(bufferRead));
//Now as you've read it, just cast the memory to struct, and assign it
item = *reinterpret_cast<PointFull*>(bufferRead);
//okay, now item holds the file content, you no longer need the buffer
还要注意:你的结构可以通过插入填充来对齐。虽然我不认为这将是PointFull的情况下,无论如何,当你需要序列化结构像这里,你最好声明它与#pragma pack
不允许填充。例如:
#pragma pack(push, 1) // exact fit - no padding
struct PointFull {
double lat;
double lon;
};
#pragma pack(pop) //back to whatever the previous packing mode was
既然你已经标记了c++
#include <iostream>
#include <fstream>
using namespace std;
struct PointFull {
double lat;
double lon;
PointFull(double lat_in = 0, double lon_in = 0)
: lat(lat_in), lon(lon_in) {}
};
int main() {
PointFull item(123123, 123123);
cout << "Writing to disk" << endl;
ofstream fout("saved_point.txt");
fout << item.lat << ' ' << item.lon;
fout.close();
cout << "Reading from disk" << endl;
PointFull item_from_disk;
ifstream fin("saved_point.txt");
fin >> item_from_disk.lat >> item_from_disk.lon;
fin.close();
cout << "From RAM and then disk" << endl;
cout << item.lat << ' ' << item.lon << endl;
cout << item_from_disk.lat << ' ' << item_from_disk.lon << endl;
return 0;
}
-
可以使用fwrite和read将数据写入文件和从文件中读取。
下面是相同的示例代码。#include <stdio.h> struct PointFull { int number; char text[10]; double real_number; }; int main() { struct PointFull data = {1, "Hello!", 3.14159}, read_data; FILE *fout = fopen("file_path", "w"); fwrite(&data, sizeof(PointFull ), 1, fout); // fprintf(fout, "%d %s %f",data.number, data.text, data.real_number); fclose(fout); FILE* fin = fopen("file_path", "r"); fread(&read_data, sizeof(PointFull ), 1, fin); printf("%d %s %lfn", read_data.number, read_data.text, read_data.real_number); fclose(fin); return 0; }
- 如果您使用fwrite,数据将以二进制或ascii格式写入。要读取数据,你必须使用read。
- 如果你使用fprintf和fscanf,数据将以人类可读格式存储。
麻省理工学院有一个叫做'drake'的库,它有这个功能。您必须采取一些步骤来序列化结构,但是它们的文档看起来非常好!
相关文章:
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 挂起和取消挂起一个文件DLL
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 如何打开并写入一个名称取自C++中字符串的文件
- 构建一个由C和C++文件组成的库
- c++多进程编写一个唯一的文件
- 如何使用CLion在Mac上创建一个新的.txt文件
- 如何将class.cpp和class.hpp编译为一个.o文件
- 写入文件会产生一个空文件或根本没有文件
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- 为什么我的 heap.h 文件给我一个LNK2001错误?
- Pantheios 不使用 MFC 应用程序写入一个文件(一个 exe + 两个 DLL)
- 在C++中一次读取一个文件一个int
- 输入/输出文件(一个字母的大写)
- 如何使用两个预编译的头文件.一个用于托管代码,另一个用于非托管代码.xxx.PCH不是一个有效的预编译头文件