hi all this is my text file.
bak kut teh[tab]888.0[tab]989.0
spicy chicken nugget[tab]999.0[tab]978.0
---------------
I'm experiencing some problem in trying to scan and put it into an array for the food names (e.g an array for food which consist of bak kut teh and spicy chicken nugget) and another 2 array for the other individual integer into C. However there seem to be some problem with my code.
(I am really a beginner)
Is there a difference if I use both tab and space instead of space for all?
Thanks for any kind of help
Code:
#include<stdio.h>
int main()
{
FILE *fp;
char food[100];
char buff[BUFSIZ];
float num;
int i;
int j=0;
if((fp=fopen("C:\\Users\\Rasyiqa\\Desktop\\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
{
for(i=0; food[i]!='\0'; i++){
if(food[i]>='a' && food[i]<='z'){
j++;
}
}
if(sscanf(buff,"food[i] %f",food,&num)==2) // getting the name and the number from the buffer which was read from the file
printf("%s %.1f\n",food,num);
else
printf("line cannot be printed\n");
}
getchar();
return 0;
}