简体   繁体   English

C程序无法正确读取文件

[英]c program cannot read the file properly

here is the code for creating and writing the file. 这是用于创建和写入文件的代码。

#include <stdio.h>

int main(void) {

FILE *cfPtr;

if((cfPtr = fopen("clients.txt","w")) == NULL) {


    puts("File could not be opened.");

}

else {

    puts("Enter the account, name, and balance.");
    puts("Enter EOF to end input.");
    printf("%s", "? ");

    unsigned int account;
    char name[30];
    double balance;

    scanf("%d %29s %lf", &account, name, &balance);
    //fprintf(cfPtr, "%d %s %f\n", account, name, balance);

    while(!feof(stdin) ) {

        fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
        printf("%s", "? ");
        scanf("%d%29s%lf", &account, name, &balance);

    }

    fclose(cfPtr);

}

return 0;

}

Here is the code for reading the file and printing the contents of txt file. 这是读取文件和打印txt文件内容的代码。

#include <stdio.h>

int main(void) {

FILE *cfPtr;

if((cfPtr = fopen("clients.txt","r")) == NULL) {


    puts("File could not be opened.");

}

else {

    unsigned int account;
    char name[30];
    double balance;

    printf("%-10s%-13s%s\n", "Account", "Name", "Balance");
    fscanf(cfPtr, "%d&29s%lf", &account, name, &balance);


    while(!feof(cfPtr)) {

        printf("%-10d%-13s%7.2f\n", account, name, balance);
        fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);

    }

    fclose(cfPtr);
}

return 0;

}

Contents of the file: 文件内容:

1 John 45.54        
2 Mike 56.65                
3 Patrick 23.32

Inputs of the writing program: 编写程序的输入:
编写程序的输入

Output of the reading program: 阅读程序的输出:
阅读程序的输出

I copied the codes from C How to Program book 8th Edition. 我从C How to Program book 8th Edition复制了代码。

What is wrong here? 怎么了

In creating and writing file code, you exiting process with ctrl+z. 在创建和编写文件代码时,使用ctrl + z退出过程。 So in this case the process is exiting without closing the file(clients.txt). 因此,在这种情况下,该过程将在不关闭文件(clients.txt)的情况下退出。 So do following changes. 以下更改也是如此。

#include <stdio.h>

int main(void) {
FILE *cfPtr;

if((cfPtr = fopen("clients.txt","a")) == NULL) {
puts("File could not be opened.");

}

else {
puts("Enter the account, name, and balance.");
puts("Enter EOF to end input.");
printf("%s", "? ");

unsigned int account;
char name[30];
double balance;

scanf("%d %29s %lf", &account, name, &balance);

while(!feof(stdin) ) {
    if((cfPtr = fopen("clients.txt","a")) == NULL) 
    {
        puts("File could not be opened.");
        break;
    }
    fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
    printf("%s", "? ");
    scanf("%d %29s %lf", &account, name, &balance);
    fclose(cfPtr);
}
    fclose(cfPtr);
}

return 0;

}

In reading file code, you have to read the data in the same format it is stored. 在读取文件代码时,您必须以存储的相同格式读取数据。 You are storing data by giving space in between variables. 通过在变量之间留出空间来存储数据。 So do following changes. 以下更改也是如此。

#include <stdio.h>

int main(void) {

FILE *cfPtr;

if((cfPtr = fopen("clients.txt","r")) == NULL) {


puts("File could not be opened.");

}

else {

unsigned int account;
char name[30];
double balance;

printf("%-10s%-13s%s\n", "Account", "Name", "Balance");
    fscanf(cfPtr, "%d %29s %lf", &account, name, &balance);


while(!feof(cfPtr)) {


    printf("%-10d%-13s%7.2f\n", account, name, balance);
    fscanf(cfPtr, "%d %29s %lf", &account, name, &balance);

}

fclose(cfPtr);
}

return 0;

}

(Posted on behalf of the OP) . (代表OP张贴)

I did not use a debugger, so I could not find a minor mistake which can be solved like this: 我没有使用调试器,所以找不到像这样可以解决的小错误:

"%d&29s%lf" -> "%d%29s%lf"

After making the change, I got the proper result. 进行更改后,我得到了正确的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM