如何在c++中写一个结构体到文件并读取文件

How to write a struct to file in C++ and read the file?

本文关键字:文件 一个 结构体 读取 c++      更新时间:2023-10-16

我有一个结构体,并使用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,但我不知道如何读取缓冲区插入数据结构?

  1. 你宁愿分配一个大小为sizeof(PointFull)的缓冲区。因为如果struct的大小被改变并且变得比你硬编码的大小大,那么你就会得到一个bug。

  2. 使用本地缓冲区,除非你真的需要使用动态内存。我假设在您的代码中并不真正需要分配。只是您可能很容易忘记释放内存,而当函数返回时缓冲区会自动删除。

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'的库,它有这个功能。您必须采取一些步骤来序列化结构,但是它们的文档看起来非常好!