文件无法在C中打开

时间:2015-03-16 19:52:42

标签: c file can-bus

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

该文件是:

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;
}

2 个答案:

答案 0 :(得分:0)

您的代码存在很多问题,但最重要的是

  1. fopen()该文件什么都不做而且fclose()它没有,我认为在MS Windows计算机上你不能再fopen(),但你做了

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

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

  4. 请注意,您在每个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;
    }
    

答案 1 :(得分:0)

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

这是一个你应该看一眼的参考资料:

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

<强>解决方案:

首先将stringfpointer转移到临时变量并检查eof(),如果允许,则将该临时string放入所需的变量。

示例:

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