C语言文件读取fscanf()
这是一个用文件记录,输出图书信息的代码,只能输出一部分,怎样循环能全部打印出来呀
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 30
const char *fname = "bookinfo.txt";
//记录图书信息结构体
typedef struct book
{
char author[MAXSIZE];
char title[MAXSIZE];
char publish_house[MAXSIZE];
}BOOK, *Book;
int Input(const char *fname);//录入信息
int Show(const char *fname);//显示信息
main()
{
Input(fname);
Show(fname);
}
int Input(const char *fname)
{
Book P_book = NULL;
char ch = 'Y';
FILE *fpw = NULL;
while('Y' == ch || 'y' == ch)
{
P_book = (Book)malloc(sizeof(BOOK));
scanf("%s",P_book->author);
scanf("%s",P_book->title);
scanf("%s",P_book->publish_house);
fpw = fopen(fname,"a");
fprintf(fpw,"%s\n%s\n%s\n",
P_book->author,P_book->title,P_book->publish_house);
fclose(fpw);
free(P_book);
printf("go on?(y or n):");
getchar();
ch = getchar();
}
return 1;
}
int Show(const char *fname)
{
char book[3][MAXSIZE] = {0};
FILE *fpr = NULL;
fpr = fopen(fname,"r");
fscanf(fpr,"%s\n%s\n%s\n",book[0],book[1],book[2]);
printf("%s %s %s\n\n",book[0],book[1],book[2]);
fclose(fpr);
}