c语言程序运行出错,求帮忙。编译没错,运行得不到正确结果。
#include<stdio.h>#include<stdlib.h>structStudent{intnum;charname[10];intscore;}stud;v...
# include <stdio.h>
# include <stdlib.h>
struct Student{
int num;
char name[10];
int score;
}stud;
void main(){
FILE * fp1;
FILE * fp2;
if((fp1=fopen("G:\\student.txt","w"))==NULL){
printf("can not open the file");
exit(0);
}
printf("please enter information:\n");
do{
fwrite(&stud,sizeof(struct Student),1,fp1);
scanf("%d %s %d",&stud.num,stud.name,&stud.score);
}while(stud.num==0);
fclose(fp1);
if((fp2=fopen("G:\\student.txt","rb"))==NULL){
printf("can not open the file");
exit(0);
}
while(!feof(fp2)){
fread(&stud,sizeof(struct Student),1,fp2);
printf("%d %s %d\n",stud.num,stud.name,stud.score);
fclose(fp2);
}
} 展开
# include <stdlib.h>
struct Student{
int num;
char name[10];
int score;
}stud;
void main(){
FILE * fp1;
FILE * fp2;
if((fp1=fopen("G:\\student.txt","w"))==NULL){
printf("can not open the file");
exit(0);
}
printf("please enter information:\n");
do{
fwrite(&stud,sizeof(struct Student),1,fp1);
scanf("%d %s %d",&stud.num,stud.name,&stud.score);
}while(stud.num==0);
fclose(fp1);
if((fp2=fopen("G:\\student.txt","rb"))==NULL){
printf("can not open the file");
exit(0);
}
while(!feof(fp2)){
fread(&stud,sizeof(struct Student),1,fp2);
printf("%d %s %d\n",stud.num,stud.name,stud.score);
fclose(fp2);
}
} 展开
展开全部
# include <stdio.h>
# include <stdlib.h>扒含宏
struct Student{
int num;
char name[10];
int score;
}stud;
void main(){
FILE * fp1;
FILE * fp2;
if((fp1=fopen("G:\\student.txt"春册,"wb"))==NULL){ //要wb
printf("can not open the file");
exit(0);
}
printf("please enter information:\n");
do{
fwrite(&stud,sizeof(struct Student),1,fp1);
scanf("%d %s %d",&stud.num,stud.name,&老慧stud.score);
}while(stud.num!=0); //不为0时循环
fclose(fp1);
if((fp2=fopen("G:\\student.txt","rb"))==NULL){
printf("can not open the file");
exit(0);
}
while(1){
fread(&stud,sizeof(struct Student),1,fp2);
if ( feof(fp2) ) //先读后检查
break ;
printf("%d %s %d\n",stud.num,stud.name,stud.score);
}
fclose(fp2); //关闭不要在循环中
}
追问
能运行吗,我现在没法用电脑,打不了
追答
已测试通过,可以运行
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询