-3

File does not show up when I enter what i want but it also doesn't give me the error.

The file is:

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;
}
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 6
    [`while (!feof(fpointer))` is always wrong.](http://stackoverflow.com/a/26557243/1983495) And how can you tell that the file did not open, if you are not checking that? – Iharob Al Asimi Mar 16 '15 at 19:53
  • Are you allowed to use functions? Question: what is the first `while` loop for? and it should be `while (fgets(singleline, sizeof(singleline), fpointer)) {}` – Iharob Al Asimi Mar 16 '15 at 20:00
  • @iharob +1 there are numerous questions about incorrect use of `feof()` – Weather Vane Mar 16 '15 at 20:06
  • There is no clear statement as to what you are trying to do, or what the problem is. What does *"File does not show up"* mean? You didn't even check the result of `fopen()`. – Weather Vane Mar 16 '15 at 20:09
  • I see you are using `float` and `if (balance == 0)` which is prone to inequality when `balance` is computed from values which cannot be exactly represented in `float`. See the very recent question http://stackoverflow.com/questions/29084695/c-comparing-floats-with-if-statements – Weather Vane Mar 16 '15 at 20:19

2 Answers2

0

Your code has a lot of problems, but the most important are

  1. You fopen() the file for doing nothing and don't fclose() it, and I think that on a MS Windows computer you cannot fopen() it again, but you do.

  2. You check if the file didn't open the second time, but not the first time, so it's like if you didn't check at all, moreover, you proceed to fclose() the file after the check has told you that it did not open.

  3. while (!feof(file)) is always wrong., so you should change the condition for your while loop.

Notice that you are repeating code in every switch case, so you should consider using a function, this is your code with all the problem fixed

#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;
}
Community
  • 1
  • 1
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

Never use eof() inside a loop condition. It's a very bad practice.

Here's a reference which you should take a peek:

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

Solution:

First take the string from fpointer to a temporary variable and check for eof() and if it permits then put that temporary string to your desired variable.

Example:

while(true){
    fgets(tmp, sizeof(tmp), fpointer);
    if(feof(fpointer)){ break;}
    text=tmp;
    }
Community
  • 1
  • 1
Jahid
  • 21,542
  • 10
  • 90
  • 108
  • There is absolutely no need to use break, at all. It makes the logic hard to follow. Your code is just `while ((text = fgets(tmp, sizeof(tmp), fpointer)) != NULL) {}`, and not even as good because if you make `char tmp[100]` your code is immediately broken. – Iharob Al Asimi Mar 16 '15 at 20:19
  • 1
    I don't see why anyone would declare char tmp[100] while they are using 150 as the size. Anyway thanks for the tip.. and yeah, I just used feof() because the OP seems to like it... – Jahid Mar 16 '15 at 20:28