簡體   English   中英

如何在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 file\n");
    return 1;
}
memcpy(buffer, &item, sizeof (item));
write(fd2, buffer, sizeof (item));

現在,我的硬盤中有一個名為“輸出”的文件,然后我想讀取該文件以測試數據。

int fd2 = open("output", O_RDONLY, S_IWUSR | S_IRUSR);
if (fd2 < 0) {
    printf("Error opening file\n");
    return 1;
}
void* bufferRead;
bufferRead = malloc(100);
read(fd2, bufferRead,100);

目前,我有bufferRead但我不知道如何讀取緩沖區以將數據插入到結構中? 任何幫助,將不勝感激。

由於您已標記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;
}
  1. 您寧願分配一個大小為sizeof(PointFull)的緩沖區。 因為如果struct的大小將被更改並且變得大於您的硬編碼大小,那么您將得到一個錯誤。

  2. 除非確實需要使用動態內存,否則請使用本地緩沖區。 我認為在您的代碼中您實際上不需要分配。 只是您可能會輕易忘記分配內存,而在函數返回時會自動刪除緩沖區。

int fd2 = open("output", O_RDONLY, S_IWUSR | S_IRUSR);
if (fd2 < 0) {
    printf("Error opening file\n");
    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
  • 您可以使用fwrite和fread將數據寫入文件或從文件讀取。
    以下是相同的示例代碼。

      #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 %lf\\n", read_data.number, read_data.text, read_data.real_number); fclose(fin); return 0; } 
    • 如果您使用fwrite,則數據將以二進制或ascii格式寫入。要讀取該數據,您必須使用fread。
    • 如果您使用fprintf和fscanf,則數據將以可人工復制的格式存儲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM