代码之家  ›  专栏  ›  技术社区  ›  Grechka Vassili

二进制文件中的读写结构[关闭]

  •  0
  • Grechka Vassili  · 技术社区  · 7 年前

    我只是想在二进制文件中写一个点结构,然后读取写的结构。

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

    #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 费希尔:测试123。bin Vous avez entr:test123。bin写入返回:0 读取返回:0 Le点est aux坐标(x,y)=(0.00,0.00)。

    我正在开发Fedora和clion IDE,

    C4 2E 18 F1 FC 7F 00 00 但是read函数不起作用(它的给定点=(0,0))

    1 回复  |  直到 7 年前
        1
  •  3
  •   Steve Summit    7 年前

    自从你 writePointToFile() readPointFromFile() 函数接受指向 Point 结构,您希望将该指针传递给 fread fwrite

    换句话说,在 writePointToFile() ,您想更改通话

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

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

    同样地 请来 .