Write pointer to file in C

后端 未结 7 1588
不思量自难忘°
不思量自难忘° 2020-12-19 09:32

I have a structure:

typedef struct student {
  char *name;
  char *surname;
  int age;
} Student;

I need to write the structure into a bina

7条回答
  •  忘掉有多难
    2020-12-19 09:48

    You need to dereference s and individually write out each element of the structure to actually write thecontents to the file:

    size_t nameCount = strlen(s->name) + 1;
    fwrite(s->name, sizeof(char), nameCount, fp);
    
    size_t surnameCount = strlen(s->surname) + 1;
    fwrite(s->surname, sizeof(char), surnameCount, fp);
    

提交回复
热议问题