I already showed you how to read a file here
http://cboard.cprogramming.com/showthread.php?t=55688
So why have yet another go at inventing something different (and still non-functional)
Code:
#include<stdio.h>
void do_write ( void ) {
char buff[BUFSIZ];
int account;
char name[30];
float money;
FILE *cfPtr;
if ((cfPtr = fopen("client.dat", "w")) == NULL)
printf("Unable to open the file");
else {
printf("Give an account number, a name and the amount of money\n");
printf("Give EOF to stop\n");
while (fgets(buff, BUFSIZ, stdin) != NULL) {
if (sscanf(buff, "%d%s%f", &account, name, &money) == 3) {
fprintf(cfPtr, "%d %s %.2f\n", account, name, money);
} else
printf("ERROR!");
}
fclose(cfPtr);
}
}
void do_read ( void ) {
char buff[BUFSIZ];
int account;
char name[30];
float money;
FILE *cfPtr;
if ((cfPtr = fopen("client.dat", "r")) == NULL)
printf("Unable to open the file");
else {
while (fgets(buff, BUFSIZ, cfPtr) != NULL) {
if (sscanf(buff, "%d%s%f", &account, name, &money) == 3) {
fprintf(stdout, "%-10d%-13s%7.2f\n", account, name, money);
} else
printf("ERROR!");
}
fclose(cfPtr);
}
}
int main ( void ) {
do_write();
do_read();
return 0;
}
Look how similar the writing and reading functions are.
With a little bit of effort, you could probably pass a few parameters to a single common function.