重构项目,困难依旧啊我!
程序代码:#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct clientData
{
int clientNum;
char telNum[9];
char firstName[10];
char lastName[15];
int tag;
}ClientData;
typedef struct billData
{
char firstName[10];
char lastName[15];
char telNum[9];
double monthFee;
double funcFee;
double cityFee;
double distanceFee;
double total;
}BillData;
int main()
{
FILE *fp1, *fp2;
ClientData client = { 0, "", "", "", 0 };
BillData bill = { "", "", "", 0, 0, 0, 0, 0 };
if( ( fp1 = fopen( "client.txt", "r+" ) ) == NULL )
{
printf( "File can not be opened\n" );
exit(1);
}
if( ( fp2 = fopen( "bill.txt", "w" ) ) == NULL )
{
printf( "\nFile can not be opened\n" );
exit(1);
}
else
{
while( fscanf( fp1, "%4d%10s%12s%15s%d", &client.clientNum, client.telNum, client.firstName, client.lastName, &client.tag ) != EOF )
{
strcpy( bill.telNum, client.telNum );
strcpy( bill.firstName, client.firstName );
strcpy( bill.lastName, client.lastName );
bill.monthFee = 26;
bill.funcFee = ( client.tag == 1 ) ? 6 : 0;
bill.cityFee = 0;
bill.distanceFee = 0;
bill.total = bill.monthFee + bill.funcFee;
fseek( fp1, 0, SEEK_END );
// fprintf( fp1, "\n" );
fprintf( fp2, "%-12s %-15s %-10s %-4.0f %-4.0f %-8.2f %-8.2f %-8.2f\n", bill.firstName, bill.lastName, bill.telNum, bill.monthFee, bill.funcFee, bill.cityFee,
bill.distanceFee, bill.total );
}
}
return 0;
}文档内容:fp1所指文档
1 82790301 yan meng 1
2 82790469 lu chengcheng 0
3 82780564 lu yin 1
4 82790203 zhang zhou 0
5 82790558 wu xu 1
6 82789098 lu jingqing 0
7 82786098 wu min 1
8 82633381 wu di 1
读取fp1的内容,经过一定的处理,放到fp2文件中。结果我值只处理了一条。我调试了下,发现while循环只循环了一次。那个fscanf函数怎么只读取一次呢????









