Originally Posted by
Salem
The problem is with your use of scanf
As soon as you input an erroneous character, scanf will fail to make use of it, and since you do nothing to trap errors, the bad character will be there next time around the loop as well. Enter infinite loop here.
Try this and forget all about scanf
Code:
char buff[BUFSIZ];
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 {
// error message
}
}
Ive tried this:
Code:
//Creates a sequential write file
#include<stdio.h>
int main ()
{
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 numer, a name and the ammount of money\n");
printf ("Give EOF to stop\n");
//scanf ("%d%s%f", &account, name, &money);
//int fflush(FILE *cfPtr);
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!");
while (!feof (stdin))
{
fprintf (cfPtr, "%d %s %.2f\n", account, name,
money);
printf ("?");
scanf ("%d%s%f", &account, name, &money);
}
fclose (cfPtr);
}
}
return 0;
}
But I still with somekind of infinite loop, unable to leave the program...
And thx a lot for the tips about scanf.