将结构写入文件&&将结构读取到动态分配的内存

时间:2018-06-04 11:57:02

标签: c structure fwrite allocation fread

我正在尝试将结构写入文件,然后将结构读取到动态分配的内存中。 如果这个algorhythm甚至可以工作,我不确定fread函数...是否能够读取以这种方式编写的文件中的所有结构并将其存储到* ptr,所以我可以从内存中访问它? 有更好的方法吗?

 typedef struct num {
    int num1;
    int num2;   
}NR;

void write() {
    int successfullywritten;
    FILE *f;
    struct num nr;
    f = fopen("dat.txt", "a");
    printf("Enter first number\n");
    scanf("%d", &nr.num1);
    printf("Enter second number\n");
    scanf("%d", &nr.num2);
    successfullywritten=fwrite(&nr, sizeof num, 1, f);
    printf("Succesfully written: %d\n", successfullywritten);
    fclose(f);
}

void read() {

    int size, entrys,i,successfullyread;
    FILE *f;
    f = fopen("dat.txt", "r");
    struct num *ptr;
    fseek(f, 0L, SEEK_END);
    size = ftell(f);
    printf("\nSize of file dat.txt: %d\n", size);
    entrys = size / sizeof(num);
    ptr = (num*)malloc(entrys * sizeof(num));
    successfullyread=fread(ptr, sizeof(num), entrys, f);
    printf("sucessfully Read: %d\n", successfullyread);
    for (i = 0;i < entrys; i++) {
        /*fread((ptr+i), sizeof(ptr), 1, f);*/
        printf("%d  ", (*(ptr)).num1);
        printf("%d\n", (*(ptr)).num2);

    }
    printf("\n");printf("\n");

    fclose(f);
}

int main() {
    int n;
    while(1){
       printf("Chooose action:\n1) write to file\n2) read from file:\n");
       scanf("%d", &n);
       switch (n) {
          case 1:
             write();
             break;
          case 2:
             read();
             break;
       }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

首先,有很多简单的错字错误,如

  • typedefed名称或结构名称之间存在混淆。这个 entrys = size / sizeof(num);应该是 entrys = size / sizeof(struct num);
  • ptr = (num*)malloc(entrys * sizeof(num));应为ptr = malloc(entrys * sizeof(struct num));。避免投射malloc()
  • 的结果

其次,找到size后,您忘记将f移至文件开头。例如

fseek(f, 0L, SEEK_END);
size = ftell(f);
fseek(f, 0L, SEEK_SET); /* you forgot to add this */ 

第三,您使用fwrite()使用ab代替a模式将数据存入文件 &安培;使用fread()阅读时使用rb代替r

从文件读取后,您需要释放ptr以避免内存泄漏。

free(ptr);

以下是示例代码

typedef struct num {
        int num1;
        int num2;
}NR;
void my_write(void) {
        int successfullywritten;
        FILE *f;
        struct num nr;
        f = fopen("dat.txt", "ab");
        printf("Enter first number\n");
        scanf("%d", &nr.num1);
        printf("Enter second number\n");
        scanf("%d", &nr.num2);
        successfullywritten=fwrite(&nr, sizeof(nr), 1, f);
        printf("Succesfully written: %d\n", successfullywritten);
        fclose(f);
}

void my_read(void) {

        int size, entrys,i,successfullyread;
        FILE *f;
        f = fopen("dat.txt", "rb");
        struct num *ptr;
        fseek(f, 0L, SEEK_END);
        size = ftell(f);
        fseek(f, 0L, SEEK_SET);
        printf("\nSize of file dat.txt: %d\n", size);
        entrys = size / sizeof(struct num);

        ptr = malloc(entrys * sizeof(struct num));
        successfullyread=fread(ptr, sizeof(struct num), 2, f);
        printf("sucessfully Read: %d\n", successfullyread);

        for (i = 0;i < entrys; i++) {
                /*fread((ptr+i), sizeof(ptr), 1, f);*/
                printf("%d  ", ptr[i].num1);
                printf("%d\n", ptr[i].num2);

        }
        free(ptr);
        printf("\n");printf("\n");
        fclose(f);
}
int main(void) {
        int n;
        while(1){
                printf("Chooose action:\n1) write to file\n2) read from file:\n");
                scanf("%d", &n);
                switch (n) {
                        case 1:
                                my_write();
                                break;
                        case 2:
                                my_read();
                                break;
                }
        }
        return 0;
}