user-0 提问: 各位大佬求助

回复:
user: user-0
content: 引用 1 楼 自信男孩 的回复:#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct Student {     char *name; //名字     int id;     int name_len; //名字长度 }Stu;  int main0101() {     //写     Stu s;     s.id = 1;     s.name_len = strlen("mike"); //名字长度     s.name = (char*)malloc(s.name_len + 1);     strcpy(s.name, "mike");     FILE* fp = fopen("selfSu.txt", "wb");     if (!fp)         return -1;      fwrite(&s, sizeof(s), 1, fp);     fclose(fp);      return 0; }  int main() {     main0101();   //先写才能再读     //读     FILE* fp = fopen("selfSu.txt", "rb");     if (!fp)         return -1;     Stu s;      fread(&s, sizeof(Stu), 1, fp);      fclose(fp);      printf("姓名：%s\t编号：%d\t名字长度：%d\n", s.name, s.id, s.name_len);      free(s.name);      return 0; }  供参考～ 引用 1 楼 自信男孩 的回复:#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct Student {     char *name; //名字     int id;     int name_len; //名字长度 }Stu;  int main0101() {     //写     Stu s;     s.id = 1;     s.name_len = strlen("mike"); //名字长度     s.name = (char*)malloc(s.name_len + 1);     strcpy(s.name, "mike");     FILE* fp = fopen("selfSu.txt", "wb");     if (!fp)         return -1;      fwrite(&s, sizeof(s), 1, fp);     fclose(fp);      return 0; }  int main() {     main0101();   //先写才能再读     //读     FILE* fp = fopen("selfSu.txt", "rb");     if (!fp)         return -1;     Stu s;      fread(&s, sizeof(Stu), 1, fp);      fclose(fp);      printf("姓名：%s\t编号：%d\t名字长度：%d\n", s.name, s.id, s.name_len);      free(s.name);      return 0; }  供参考～ 引用 1 楼 自信男孩 的回复:#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct Student {     char *name; //名字     int id;     int name_len; //名字长度 }Stu;  int main0101() {     //写     Stu s;     s.id = 1;     s.name_len = strlen("mike"); //名字长度     s.name = (char*)malloc(s.name_len + 1);     strcpy(s.name, "mike");     FILE* fp = fopen("selfSu.txt", "wb");     if (!fp)         return -1;      fwrite(&s, sizeof(s), 1, fp);     fclose(fp);      return 0; }  int main() {     main0101();   //先写才能再读     //读     FILE* fp = fopen("selfSu.txt", "rb");     if (!fp)         return -1;     Stu s;      fread(&s, sizeof(Stu), 1, fp);      fclose(fp);      printf("姓名：%s\t编号：%d\t名字长度：%d\n", s.name, s.id, s.name_len);      free(s.name);      return 0; }  供参考～   谢谢~

user: user-1
content: 哦，名字也没有写进去。 题目需要你另外写名字，不能光写结构体。

user: user-1
content: 显然你缺少了malloc，所以名字没地方放。

user: user-2
content: 引用 1 楼 自信男孩 的回复:#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct Student {     char *name; //名字     int id;     int name_len; //名字长度 }Stu;  int main0101() {     //写     Stu s;     s.id = 1;     s.name_len = strlen("mike"); //名字长度     s.name = (char*)malloc(s.name_len + 1);     strcpy(s.name, "mike");     FILE* fp = fopen("selfSu.txt", "wb");     if (!fp)         return -1;      fwrite(&s, sizeof(s), 1, fp);     fclose(fp);      return 0; }  int main() {     main0101();   //先写才能再读     //读     FILE* fp = fopen("selfSu.txt", "rb");     if (!fp)         return -1;     Stu s;      fread(&s, sizeof(Stu), 1, fp);      fclose(fp);      printf("姓名：%s\t编号：%d\t名字长度：%d\n", s.name, s.id, s.name_len);      free(s.name);      return 0; }  供参考～ 楼主这么定义结构体和读取很容易导致内存溢出～在main函数里释放了。

user: user-2
content: #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> #include<string.h>  typedef struct Student {     char *name; //名字     int id;     int name_len; //名字长度 }Stu;  int main0101() {     //写     Stu s;     s.id = 1;     s.name_len = strlen("mike"); //名字长度     s.name = (char*)malloc(s.name_len + 1);     strcpy(s.name, "mike");     FILE* fp = fopen("selfSu.txt", "wb");     if (!fp)         return -1;      fwrite(&s, sizeof(s), 1, fp);     fclose(fp);      return 0; }  int main() {     main0101();   //先写才能再读     //读     FILE* fp = fopen("selfSu.txt", "rb");     if (!fp)         return -1;     Stu s;      fread(&s, sizeof(Stu), 1, fp);      fclose(fp);      printf("姓名：%s\t编号：%d\t名字长度：%d\n", s.name, s.id, s.name_len);      free(s.name);      return 0; }  供参考～