正在学c语言,改了一下网课的代码,发现输入的和输出的全是乱码
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#pragma warning(disable : 4996)
struct Student
{
char name[20];
int num;
int age;
float score;
};
int main(void)
{
struct Student boys[3];
struct Student boy;
struct Student* pBoys;
FILE* fp;
pBoys = boys;
fp = fopen("text.txt", "wb+");
if (fp == NULL)
{
printf("不能打开该文件。\n");
getch();
exit(0);
}
printf("请输入学生的相关数据:\n");
for (size_t i = 0; i < 3; i++)
{
scanf("%s %d %d %f", pBoys->name, &pBoys->num, &pBoys->age, &pBoys->score);
pBoys++;
}
fwrite(boys, sizeof(struct Student), 3, fp);
if (ferror(fp))
{
puts("读取出错");
}
else
{
puts("读取成功");
}
fseek(fp, sizeof(struct Student), SEEK_SET);
fread(boys, sizeof(struct Student),3, fp);
for (size_t i = 0; i < 3; i++)
{
printf("%s %d %d %f\n", pBoys->name, &pBoys->num, &pBoys->age, &pBoys->score);
pBoys++;
}
fclose(fp);
return 0;
}
请输入学生的相关数据:
a 10 10 10
b 10 10 10
c 10 10 10
读取成功
烫烫;8z< 15726888 15726892 0.000000
仞8?橓?15726920 15726924 0.000000
15726952 15726956 0.000000
G:\C++练习\结构体\文件\Debug\文件示例函数fseek.exe (进程 15520)已退出,代码为 0。
按任意键关闭此窗口. . .
试过fprint和fscanf代码,也是了把fseek换成rewind(fp),都不行,诊断之后,发现fwrite之后,文档里全变成了乱码
本来是想逐条输出结构体内容,但是怎么改都不行
你的结构体对象有一个数组就行了,输出的时候不用加取地址符
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#pragma warning(disable : 4996)
struct Student
{
char name[20];
int num;
int age;
float score;
};
int main(void)
{
struct Student boys[3];
FILE* fp;
fp = fopen("text.txt", "wb+");
if (fp == NULL)
{
printf("不能打开该文件。\n");
getch();
exit(0);
}
printf("请输入学生的相关数据:\n");
for (int i = 0; i < 3; i++)
{
scanf("%s %d %d %f", boys[i].name, &boys[i].num, &boys[i].age,& boys[i].score);
}
fwrite(boys, sizeof(struct Student), 3, fp);
if (ferror(fp))
{
puts("读取出错");
}
else
{
puts("读取成功");
}
fseek(fp, sizeof(struct Student), SEEK_SET);
fread(boys, sizeof(struct Student),3, fp);
for (int i = 0; i < 3; i++)
{
printf("%s %d %d %f\n", boys[i].name, boys[i].num, boys[i].age, boys[i].score);
}
fclose(fp);
return 0;}
printf("%s %d %d %f\n", pBoys->name, &pBoys->num, &pBoys->age, &pBoys->score);
pBoys指向位置不对啊,28行前加一句,pBoys = boys; pBoys需要重新指向boys数组的开始位置。因为前面输入的时候已经将pBoys++指向了数组尾部了
另外printf语句也错误,非字符串不用加&符号。
printf("%s %d %d %f\n", pBoys->name, pBoys->num, pBoys->age, pBoys->score);
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#pragma warning(disable : 4996)
struct Student
{
char name[20];
int num;
int age;
float score;
};
int main(void)
{
struct Student boys[3];
struct Student boy;
struct Student* pBoys;
FILE* fp;
pBoys = boys;
fp = fopen("text.txt", "wb+");
if (fp == NULL)
{
printf("不能打开该文件。\n");
getch();
exit(0);
}
printf("请输入学生的相关数据:\n");
for (size_t i = 0; i < 3; i++)
{
scanf("%s %d %d %f", pBoys->name, &pBoys->num, &pBoys->age, &pBoys->score);
pBoys++;
}
fwrite(boys, sizeof(struct Student), 3, fp);
if (ferror(fp))
{
puts("读取出错");
}
else
{
puts("读取成功");
}
fseek(fp, sizeof(struct Student), SEEK_SET);
fread(boys, sizeof(struct Student),3, fp);
pBoys = boys;
for (size_t i = 0; i < 3; i++)
{
printf("%s %d %d %f\n", pBoys->name, pBoys->num, pBoys->age, pBoys->score);
pBoys++;
}
fclose(fp);
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!