用c语言将结构体写入文件

时间:2021-11-09 00:04:25

Is it possible to write an entire struct to a file

是否可以将整个结构体写入文件

example:

例子:

struct date {
    char day[80];
    int month;
    int year;
};

1 个解决方案

#1


27  

Is it possible to write an entire struct to a file

是否可以将整个结构体写入文件

Your question is actually writing struct instances into file.

您的问题实际上是将struct实例写入文件。

  1. You can use fwrite function to achieve this.
  2. 您可以使用fwrite函数来实现这一点。
  3. You need to pass the reference in first argument.
  4. 您需要在第一个参数中传递引用。
  5. sizeof each object in the second argument
  6. 第二个参数中的每个对象的sizeof
  7. Number of such objects to write in 3rd argument.
  8. 在第3个参数中写入这些对象的数量。
  9. File pointer in 4th argument.
  10. 第四个参数中的文件指针。
  11. Don't forget to open the file in binary mode.
  12. 不要忘记以二进制模式打开文件。
  13. You can read objects from file using fread.
  14. 可以使用fread从文件中读取对象。
  15. Careful with endianness when you are writing/reading in little endian systems and reading/writing in big endian systems and viceversa. Read how-to-write-endian-agnostic-c-c-code

    当你在小endian系统中写/读,在大endian系统和viceversa系统中读/写时,要小心机缘巧合。读how-to-write-endian-agnostic-c-c-code

    struct date *object=malloc(sizeof(struct date));
    strcpy(object->day,"Good day");
    object->month=6;
    object->year=2013;
    FILE * file= fopen("output", "wb");
    if (file != NULL) {
        fwrite(object, sizeof(struct date), 1, file);
        fclose(file);
    }
    

You can read them in the same way....using fread

你可以阅读他们以同样的方式....使用从文件中读

    struct date *object2=malloc(sizeof(struct date));
    FILE * file= fopen("output", "rb");
    if (file != NULL) {
        fread(object2, sizeof(struct date), 1, file);
        fclose(file);
    }
    printf("%s/%d/%d\n",object2->day,object2->month,object2->year);

#1


27  

Is it possible to write an entire struct to a file

是否可以将整个结构体写入文件

Your question is actually writing struct instances into file.

您的问题实际上是将struct实例写入文件。

  1. You can use fwrite function to achieve this.
  2. 您可以使用fwrite函数来实现这一点。
  3. You need to pass the reference in first argument.
  4. 您需要在第一个参数中传递引用。
  5. sizeof each object in the second argument
  6. 第二个参数中的每个对象的sizeof
  7. Number of such objects to write in 3rd argument.
  8. 在第3个参数中写入这些对象的数量。
  9. File pointer in 4th argument.
  10. 第四个参数中的文件指针。
  11. Don't forget to open the file in binary mode.
  12. 不要忘记以二进制模式打开文件。
  13. You can read objects from file using fread.
  14. 可以使用fread从文件中读取对象。
  15. Careful with endianness when you are writing/reading in little endian systems and reading/writing in big endian systems and viceversa. Read how-to-write-endian-agnostic-c-c-code

    当你在小endian系统中写/读,在大endian系统和viceversa系统中读/写时,要小心机缘巧合。读how-to-write-endian-agnostic-c-c-code

    struct date *object=malloc(sizeof(struct date));
    strcpy(object->day,"Good day");
    object->month=6;
    object->year=2013;
    FILE * file= fopen("output", "wb");
    if (file != NULL) {
        fwrite(object, sizeof(struct date), 1, file);
        fclose(file);
    }
    

You can read them in the same way....using fread

你可以阅读他们以同样的方式....使用从文件中读

    struct date *object2=malloc(sizeof(struct date));
    FILE * file= fopen("output", "rb");
    if (file != NULL) {
        fread(object2, sizeof(struct date), 1, file);
        fclose(file);
    }
    printf("%s/%d/%d\n",object2->day,object2->month,object2->year);