【发布时间】:2015-09-16 12:50:39
【问题描述】:
我开始学习用 C 语言处理文件。这个特定程序的重点是创建一个名为“clients.dat”的文件,我在其中存储银行客户的帐号、姓名和余额,比如说.我已经对代码进行了工作和改进,使其完美复制了教科书作为示例提供的内容,但由于某种原因,我的代码在第一个“scanf”之后无休止地循环并重新打印问号以被遗忘,而从未进入 scanf while 循环内的语句。有人会对为什么会发生这种情况有任何建议吗?我的编译器是 Netbeans,我在 Linux-Ubuntu 上运行它。
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
int main() {
unsigned int actNumber;
char actName[30];
long double actBalance;
FILE *fPtr;
if((fPtr = fopen("clients.dat", "w")) == NULL) {
printf("File could not be found.\n");
}
else {
printf("Enter the Account Number, Name, and Balance.\n Hit the EoF to exit.\n");
printf("%s","?");
scanf("%d%29s%lf", &actNumber, actName, &actBalance);
while (!feof(stdin)) {
fprintf(fPtr, "%d, %29s, %.2lf\n", actNumber, actName, actBalance);
printf("%s", "?");
scanf("%d%29s%lf", &actNumber, actName, &actBalance);
}
fclose(fPtr);
}
return;
}
【问题讨论】:
-
while (!feof(stdin)) {表示您需要按键盘上的 Ctrl-D 来终止标准输入流 -
为了澄清 Netbeans 不是你的编译器,而是你的 IDE。 Netbeans 使用一些底层 C 编译器来构建您的代码。
-
@fukanchik 呃,我知道吗?它仍然是错误的。阅读链接的答案。