課后一道作業題:
1. 將以下結構體型別變數,以二進制的形式存放到檔案中,并且可以實作讀的介面,列印讀出來資訊。
typedef struct Student
{
char *name; //名字
int id;
int name_len; //名字長度
}Stu;
Stu s; //定義結構體變數
//結構體成員賦值
s.id = 1;
s.name_len = strlen("mike"); //名字長度
s.name = (char *)malloc(s.name_len + 1);
strcpy(s.name, "mike");
然后我的代碼如下:
#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()
{
//讀
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);
return 0;
}
為什么一直報錯呀?錯誤如下:
uj5u.com熱心網友回復:
#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;
}
供參考~
uj5u.com熱心網友回復:
參考 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函式里釋放了。
uj5u.com熱心網友回復:
顯然你缺少了malloc,所以名字沒地方放。
uj5u.com熱心網友回復:
哦,名字也沒有寫進去。
題目需要你另外寫名字,不能光寫結構體。
uj5u.com熱心網友回復:
參考 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;
}
供參考~
謝謝~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/251092.html
標籤:C語言
上一篇:求教大佬
下一篇:新手,錯誤提示none of the 2 overloads can convert parameter 1 from type 'float'是怎么回事ne