誰能告訴我在這個程序中的漏洞......?其實它是兩次打印最後一個記錄。棟篤神探缺陷
#include <stdio.h>
int main(void)
{
int accountNum;
char name[30];
double balance;
int counter = 0;
FILE *clientDataFile1;
if((clientDataFile1 = fopen("clients.txt", "r")) == NULL)
printf("File could not be opened");
else
{
printf("%-10s %-13s %s\n", "Account", "Name", "Balance");
while(!feof(clientDataFile1))
{
fscanf(clientDataFile1, "%d%s%lf", &accountNum, name, &balance);
printf("%-10d%-13s%.2lf\n", accountNum, name, balance);
}
printf("\n\n\n");
rewind(clientDataFile1);
counter++;
fclose(clientDataFile1);
}
return 0;
}
這是變得非常painful.I嘗試了很多次,但缺陷是抓不到。要麼我的工作不清楚,要麼Ubuntu 12.10或gcc對此負責。 幫我......
爲什麼你倒帶只是爲了關閉它?文件不是VHS磁帶。 – Kevin
輕微:建議'「%d%29s%lf」'以防止'name'溢出。 – chux