【问题标题】:Write - Read structure in binary file [closed]写入 - 读取二进制文件中的结构 [关闭]
【发布时间】:2017-09-08 19:25:38
【问题描述】:

我只是想在二进制文件中写入一个 Point 结构并读取写入的结构。

有人能解释一下为什么这段代码不起作用吗?

#include<stdio.h>
#define N 100

typedef struct Point Point;
struct Point{
    float x;
    float y;
};

void initialisePoint(Point * p);
void showPoint(Point * p);
void initialiseFileName(char * fileName);
int writePointToFile(char * fileName, Point * p);
int readPointFromFile(char * fileName, Point * p);

int main()
{
    Point p1 = {0};
    Point p2 = {0};
    char fileName[N] = {0};
    int exitStatus = 0;

    initialisePoint(&p1);
    showPoint(&p1);

    initialiseFileName(fileName);

    printf("Vous avez entré : %s\n", fileName);

    printf("Write return : %d\n", writePointToFile(fileName, &p1));

    printf("Read return : %d\n", readPointFromFile(fileName, &p2));

    showPoint(&p2);

    return exitStatus;
}

void initialisePoint(Point * p){
    printf("Entrez une valeur pour x : ");
    scanf("%f", &p->x);
    printf("Entrez une valeur pour y : ");
    scanf("%f", &p->y);
}
void showPoint(Point * p){
    printf("Le point est aux coordonnées (x,y) = (%.2lf, %.2lf).\n", p->x, p->y);
}
void initialiseFileName(char * fileName){
    printf("Entrez une valeur pour le nom de fichier : ");
    scanf("%s", fileName);
}
int writePointToFile(char * fileName, Point * p)
{
    FILE* f2 = NULL;

    f2 = fopen(fileName, "wb");

    if(f2 != NULL){
        if(fwrite(&p, sizeof(struct Point), 1, f2) != 1){
            printf("Could not write to file.");
            fclose(f2);
            return -1;
        }
    }
    else
    {
        printf("Could not open file.");
        return -1;
    }
    fclose(f2);
    return 0;
}
int readPointFromFile(char * fileName, Point * p){

    FILE* f = NULL;

    f = fopen(fileName, "rb");

    if(f != NULL){
        if(fread(&p, sizeof(struct Point), 1, f) != 1){
            printf("Could not read from file.");
            fclose(f);
            return -1;
        }
    }
    else{
        printf("Could not open file.");
        return -1;
    }
    fclose(f);
    return 0;
}

这是我的日志:

/home/sviktor/CLionProjects/untitled/cmake-build-debug/untitled Entrez une valeur pour x : 2.33 Entrez une valeur pour y : 1.34 Le point est 辅助坐标 (x,y) = (2.33, 1.34)。 Entrez une valeur pour le nom de fichier:test123.bin Vous avez entré:test123.bin 写返回:0 读取返回:0 Le point est aux coordonnées (x,y) = (0.00, 0.00)。

进程以退出代码 0 结束

我正在开发 Fedora 和 clion IDE,

Bless 编辑器说我的 test123.bin 文件包含这个 C4 2E 18 F1 FC 7F 00 00 但读取功能不起作用(它给出点 = (0,0))

【问题讨论】:

  • 如果您使用 hexeditor 检查您的输出文件,您会看到什么?写对了吗?
  • “不工作”是什么意思?
  • 当调用freadfwrite时,尝试传递p而不是&amp;p作为第一个参数。 p是结构体的地址; &amp;p 是保存结构地址的指针变量的地址。我想后者有点过分了。
  • fread(&amp;p, sizeof(struct Point) ==> fread(p, sizeof(struct Point) - 注意第一个参数。
  • 尽管如此,它还有更多的错误。通常,您应该将结构写入这样的文件。无法保证结构的实际布局,因此很有可能在另一个编译器或同一编译器的更高版本或具有不同选项的完全相同的编译器上重新编译您的程序会使您的程序与您保存的文件不兼容.您应该实现proper serialization,以受控方式写入值。

标签: c binaryfiles


【解决方案1】:

由于您的writePointToFile()readPointFromFile() 函数接受指向Point 结构的指针,因此您希望将该指针传递给freadfwrite。它是您的数据的指针,这是您要读取和写入的内容。您的代码错误地传递了指向该指针的指针。

也就是说,在writePointToFile(),你想换个电话

fwrite(&p, sizeof(struct Point), 1, f2)

fwrite(p, sizeof(struct Point), 1, f2)

对于readPointFromFile() 中的fread 调用也是如此。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多