I have the following code:
Code:
#include<stdio.h>
int main()
{
FILE *fp;
char name[25];
char buff[BUFSIZ];
float num;
if((fp=fopen("test.txt","r"))==NULL)
{
perror("File cannot be opened");
getchar();
exit(1);
}
while(fgets(buff,sizeof(buff),fp)!=NULL) // read a single line from the file till i get EOF
{
if(sscanf(buff,"%s %f",name,&num)==2) // getting the name and the number from the buffer which was read from the file
printf("%s %.1f\n",name,num);
else
printf("line cannot be printed\n");
}
getchar();
return 0;
}
and test.txt:
Elsewhere 22.65 11.95
Overthere 55
Sidknee 147 34
Since "55" isn't a valid double, i don't want to print any of the records. What's happening now is:
Sarah 22.65 11.95
line cannot be printed
Sandy 147.00 34.00
Is there anyway when theres a int found to exit and print nothing but an error message?