本帖最后由 jackz007 于 2021-3-12 11:48 编辑
此句有错 得改成这样 scanf("%d" , & stu . age) ;
没必要用递归void StuInput()
{
. . . . . .
if(choice == 'y')
{
StuInput(); // 这是递归吗?
}
}
这是我修改的代码#include <stdio.h>
void StuInput(char fn[])
{
char name[64] ;
int num , age ;
FILE * fp ;
if((fp = fopen(fn , "a+"))) {
printf("\n") ;
printf("***********Student Record System***********\n") ;
for(;;) {
printf("Please input num name age : ") ;
scanf("%d%s%d" , & num , name , & age) ;
if(num > 0) fprintf(fp , "%d %s %d\n" , num , name , age) ;
else break ;
}
fclose(fp) ;
} else {
fprintf(stderr , "\n") ;
fprintf(stderr , "Can't open input file : %s\n" , fn) ;
fprintf(stderr , "\n") ;
}
}
void StuPrint(char fn[])
{
char name[64] ;
int num , age ;
FILE * fp ;
if((fp = fopen(fn , "r"))) {
printf("\n") ;
printf("***********[ %s ]***********\n" , fn) ;
fscanf(fp , "%d %s %d" , & num , name , & age) ;
for(; ! feof(fp) ;) {
printf("%d %s %d\n" , num , name , age) ;
fscanf(fp , "%d %s %d" , & num , name , & age) ;
}
fclose(fp) ;
} else {
fprintf(stderr , "\n") ;
fprintf(stderr , "Can't open input file : %s\n" , fn) ;
fprintf(stderr , "\n") ;
}
}
int main(void)
{
char fn[] = "text1.txt" ;
StuInput(fn) ;
StuPrint(fn) ;
}
编译、运行实况D:\0002.Exercise\C>g++ -o x x.c
D:\0002.Exercise\C>x
***********Student Record System***********
Please input num name age : 100 abc 18
Please input num name age : 101 bcd 19
Please input num name age : 102 cde 20
Please input num name age : 103 def 21
Please input num name age : 0 0 0
***********[ text1.txt ]***********
100 abc 18
101 bcd 19
102 cde 20
103 def 21
D:\0002.Exercise\C>
|