0

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.

#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:
Inputs of the writing program

Output of the reading program:
Output of the reading program

I copied the codes from C How to Program book 8th Edition.

What is wrong here?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Please take some time to read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude May 23 '17 at 06:41
  • 4
    `"%d&29s%lf"` -> `"%d%29s%lf"`. You would be able to find that yourself if you do basic function return value/error checks and basic debugging (ie, use a debugger). – kaylum May 23 '17 at 06:42
  • @kaylum Shouldn't `%d` be `%u` since its argument is `unsigned`? – Spikatrix May 23 '17 at 06:49
  • @kaylum I use windows 8.1, where can i learn how to find and use a debugger? –  May 23 '17 at 06:51
  • @JacobWarbler you can get visual studio or codeblocks they have debugger with them or download mingw package – codeconscious May 23 '17 at 06:54
  • @DenisKa Thank you. –  May 23 '17 at 06:55
  • Jacob, just a quick note to say that we do not add answers to questions here - add them to an answer please. This was especially necessary in your case, since you added the answer _before_ your question and not after it, and it was not marked as an update. Future readers would have had a hard time working out what part of the post was question and what was its solution. Hope this feedback helps! – halfer May 23 '17 at 16:33

2 Answers2

0

In creating and writing file code, you exiting process with ctrl+z. So in this case the process is exiting without closing the file(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;

}
  • I edited the question, could you please look at it again? I suppose problem solved after making change that i explained in edit. –  May 23 '17 at 07:59
  • Yeah, that was the main problem. But in "creating and writing file" code you are terminating process by ctrl+z, it causes improper termination of file (clients.txt). I tested your code in linux platform. After ctrl+z the file was empty. So I tried like this. – Darshan Rao May 23 '17 at 08:43
  • In my book it says " z then press Enter" for windows, " d" for Linux/Mac OS X/UNIX. –  May 23 '17 at 08:53
  • Yes, it is going well now with ctrl+d. It's good to learn new things. Thank you – Darshan Rao May 23 '17 at 09:05
0

(Posted on behalf of the 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.

halfer
  • 19,824
  • 17
  • 99
  • 186