File does not open in C -
file not show when enter want doesn't give me 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 not opened\n"); else { printf("enter request\n" "1 - list accounts 0 balances\n" "2 - list accounts credit balances\n" "3 - list accounts 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 0 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 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 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; }
your code has lot of problems, important are
you
fopen()file doing nothing , don'tfclose()it, , think on ms windows computer cannotfopen()again, do.you check if file didn't open second time, not first time, it's if didn't check @ all, moreover, proceed
fclose()file after check has told did not open.while (!feof(file))wrong., should change condition while loop.
notice repeating code in every switch case, should consider using function, code 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 not opened\n"); else { int chr; char line[100]; printf("enter request\n" "1 - list accounts 0 balances\n" "2 - list accounts credit balances\n" "3 - list accounts 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 0 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 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 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; }
Comments
Post a Comment