C语言数据块读写
看看哪错了?运行后输入1遍就被退了出来,写入的数据只有前2个比较符合,中间还有些乱码。。从书上抄的,怎么还有错啊,?#include<stdio.h>#include<s...
看看哪错了?运行后输入1遍就被退了出来,写入的数据只有前2个比较符合,中间还有些乱码。。从书上抄的,怎么还有错啊,?
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1;
int i;
struct stu
{
char name[15];
char num[6];
float score[2];
} student;
if((fp1=fopen("test.txt","wb"))==NULL)
{
printf("cannot open file");
exit(0);
}
printf("input data:\n");
for(i=0;i<2;i++)
{ scanf("%s%s%f%f",student.name,student.num,&student.score[0],&student.score[1]);
fwrite(&student,sizeof(student),1,fp1);
fclose(fp1);
if((fp1=fopen("text.txt","rb"))==NULL)
{
printf("cannot open file");
exit(0);
}
printf("out from file:\n");
for(i=0;i<2;i++)
{ fread(&student,sizeof(student),1,fp1);
printf("%s%s%7.2f%7.2f\n",student.name,student.num,student.score[0],student.score[1]);
}
fclose(fp1);
return 0;
}}
if((fp1=fopen("text.txt","rb"))==NULL)
{
printf("cannot open file");
exit(0);
}
这是什么意思啊?不能直接打开文件用fp1=fopen("text.txt","rb");么? 展开
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp1;
int i;
struct stu
{
char name[15];
char num[6];
float score[2];
} student;
if((fp1=fopen("test.txt","wb"))==NULL)
{
printf("cannot open file");
exit(0);
}
printf("input data:\n");
for(i=0;i<2;i++)
{ scanf("%s%s%f%f",student.name,student.num,&student.score[0],&student.score[1]);
fwrite(&student,sizeof(student),1,fp1);
fclose(fp1);
if((fp1=fopen("text.txt","rb"))==NULL)
{
printf("cannot open file");
exit(0);
}
printf("out from file:\n");
for(i=0;i<2;i++)
{ fread(&student,sizeof(student),1,fp1);
printf("%s%s%7.2f%7.2f\n",student.name,student.num,student.score[0],student.score[1]);
}
fclose(fp1);
return 0;
}}
if((fp1=fopen("text.txt","rb"))==NULL)
{
printf("cannot open file");
exit(0);
}
这是什么意思啊?不能直接打开文件用fp1=fopen("text.txt","rb");么? 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询