【问题标题】:File does not open in C文件无法在 C 中打开
【发布时间】:2015-03-16 19:52:41
【问题描述】:

当我输入我想要的内容时文件没有显示,但它也没有给我错误。

文件是:

Account Name    Balance
100 Jones   24.98
200 Doe     345.67
300 White   0.00
400 Stone   -42.16
500 Rich    224.62
int main(void)
{
    int account;
    char name[30];
    float balance;
    int request;
    char singleline[150];
    FILE * fpointer;


    fpointer = fopen("acc.txt","r");
    while (!feof(fpointer))
    {
        fgets(singleline, 150, fpointer);
    }
    if ((fpointer= fopen("acc.txt", "r"))==NULL)
        printf("File could not be opened\n");
    else
    {
        printf("Enter Request\n"
               "1 - List accounts with zero balances\n"
               "2 - List accounts with credit balances\n"
               "3 -  List accounts with debit balances\n"
               "4 - Endof run\n");
        scanf("%d", &request);
        while (request !=4)
        {
            fscanf(fpointer,"%d%s%f", &account,name, &balance);
            switch(request)
            {
            case 1:
                printf("\nAccounts with zero balnces:\n");
                while(!feof(fpointer))
                {
                    if (balance ==0)
                        printf("%-10d%-13s%7.2f\n", account, name, balance);
                    fscanf(fpointer,"%d%s%f", &account,name, &balance);
                }
                break;
            case 2:
                printf("\nAccounts with credit balances:\n");
                while(!feof(fpointer))
                {
                    if (balance<0)
                        printf("%-10d%-13s%7.2f\n", account, name, balance);
                    fscanf(fpointer,"%d%s%f", &account,name, &balance);
                }
                break;
            case 3:
                printf("\nAccounts with debit balances:\n");
                while(!feof(fpointer))
                {
                    if (balance>0)
                        printf("%-10d%-13s%7.2f\n", account, name, balance);
                    fscanf(fpointer,"%d%s%f", &account,name, &balance);
                }
                break;
            }
            rewind (fpointer);
            printf("\n?" );
            scanf("%d",&request);
        }
        printf("End of run.\n");
    }
    fclose(fpointer);
    system("pause >nul");
    return 0;
}

【问题讨论】:

  • while (!feof(fpointer)) is always wrong. 如果你不检查,你怎么知道文件没有打开?
  • 你可以使用函数吗?问题:第一个while 循环是做什么用的?它应该是while (fgets(singleline, sizeof(singleline), fpointer)) {}
  • @iharob +1 有很多关于不正确使用feof()的问题
  • 没有明确说明您要做什么,或者问题是什么。 “文件不显示” 是什么意思?你甚至没有检查fopen()的结果。
  • 我看到您正在使用floatif (balance == 0),当balance 是根据无法在float 中精确表示的值计算时容易出现不等式。查看最近的问题stackoverflow.com/questions/29084695/…

标签: c file can-bus


【解决方案1】:

你的代码有很多问题,但最重要的是

  1. fopen()文件什么都不做,不要fclose()它,我认为在MS Windows计算机上你不能再次fopen()它,但你可以。

  2. 你第二次检查文件是否没有打开,但第一次没有打开,所以就像你根本没有检查一样,而且检查后你继续fclose()文件已经告诉你它没有打开。

  3. while (!feof(file)) is always wrong.,因此您应该更改 while 循环的条件。

请注意,您在每个switch 情况下都在重复代码,因此您应该考虑使用一个函数,这是您的所有问题都已修复的代码

#include <stdio.h>

int main(void)
{
    int   account;
    char  name[30];
    float balance;
    int   request;
    FILE *fpointer;


    if ((fpointer = fopen("acc.txt", "r")) == NULL)
        printf("File could not be opened\n");
    else
    {
        int  chr;
        char line[100];
        printf("Enter Request\n"
               "1 - List accounts with zero balances\n"
               "2 - List accounts with credit balances\n"
               "3 -  List accounts with debit balances\n"
               "4 - Endof run\n");
        scanf("%d", &request);       
        while ((request != 4) && (fgets(line, sizeof(line), fpointer) != NULL))
        {
            switch(request)
            {
            case 1:
                printf("\nAccounts with zero balnces:\n");
                while(fscanf(fpointer,"%d%s%f", &account, name, &balance) == 3)
                {
                    if (balance == 0)
                        printf("%-10d%-13s%7.2f\n", account, name, balance);
                }
                break;
            case 2:
                printf("\nAccounts with credit balances:\n");
                while(fscanf(fpointer,"%d%s%f", &account, name, &balance) == 3)
                {
                    if (balance < 0)
                        printf("%-10d%-13s%7.2f\n", account, name, balance);
                }
                break;
            case 3:
                printf("\nAccounts with debit balances:\n");
                while(fscanf(fpointer,"%d%s%f", &account, name, &balance) == 3)
                {
                    if (balance > 0)
                        printf("%-10d%-13s%7.2f\n", account, name, balance);
                }
                break;
            }
            rewind (fpointer);
            printf ("\n?" );

            while (((chr = getchar()) != EOF) && (chr != '\n'));
            scanf("%d", &request);
        }
        printf("End of run.\n");
        fclose (fpointer);
    }

    return 0;
}

【讨论】:

    【解决方案2】:

    切勿在循环条件中使用eof()。这是一种非常糟糕的做法。

    这是你应该看一看的参考:

    Why is “while ( !feof (file) )” always wrong?

    解决方案:

    首先将fpointer 中的string 放入一个临时变量并检查eof(),如果允许,然后将该临时string 放入您想要的变量中。

    例子:

    while(true){
        fgets(tmp, sizeof(tmp), fpointer);
        if(feof(fpointer)){ break;}
        text=tmp;
        }
    

    【讨论】:

    • 完全没有必要使用break。这使得逻辑难以遵循。你的代码只是while ((text = fgets(tmp, sizeof(tmp), fpointer)) != NULL) {},甚至没有那么好,因为如果你让char tmp[100]你的代码会立即被破坏。
    • 我不明白为什么有人会在使用 150 作为大小时声明 char tmp[100]。无论如何感谢您的提示.. 是的,我只是使用 feof() 因为 OP 似乎喜欢它...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多