哪位大侠帮忙给解释一下(文件读取问题)
创建文件代码:void main()
{
int account;
char name[ 30 ];
double balance;
FILE *cfPtr;
if( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ){
printf( "File could not be opened\n" );
}
else{
printf( "Enter account, name and balance\n" );
printf( "Enter EOF to end\n" );
printf( "? " );
scanf( "%d%s%lf", &account, name, &balance );
}
while( !feof( stdin ) ){
fprintf( cfPtr, "%d %s %.2f", account, name, balance );
printf( "? " );
scanf( "%d%s%lf", &account, name, &balance );
}
fclose( cfPtr );
}
读文件代码:
#include "stdio.h"
void main()
{
int account;
char name[ 30 ];
double balance;
FILE *cfPtr;
if( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ){
printf( "File could not be opened\n" );
}
else{
printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" );
fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
}
while( !feof( cfPtr ) ){
printf( "%-10d%-13s%7.2f\n", account, name, balance );
fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
}
fclose( cfPtr );
}
但是运行的时候为什么不停地读出文件中的第一条信息,后面的为什么没有,本人是初学者,还请各位前辈不吝赐教!!不胜感激!!!