简体   繁体   中英

Reading separated data from file to different data types in C language

I have trouble when I read these data into (int string float) in C language,
the compiler read the data after the first comma until the end of the line into string (char[])
and do not store any data in a float variable. Hope it understood.
thanks

Blockquote
00979,Jack,133.2
11411,Tony,615.3
24451,Jasmen,655.9
77321,Ahmad,821
09022,Diana,179.3
19091,Tayma,776.6

my code

int flag =1,seat ;
char str[10];
float avg ;
while (flag !=EOF ){
    flag=fscanf(in,"%d , %s , %f",&seat,str,&avg);
    if(flag==EOF){
        break ;
    }
    printf("%d    %s    %f\n",seat,str,avg);
}  

You should use %[^,] specifier instead of %s specifier to read string until next comma.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM