How to use scanf \ fscanf to read a line and parse into variables?

前端 未结 2 1091
天命终不由人
天命终不由人 2021-01-03 06:39

I\'m trying to read a text file built with the following format in every line:

char*,char*,int

i.e.:

aaaaa,dfdsd,23

bbbasdaa,ffffd,100

2条回答
  •  [愿得一人]
    2021-01-03 06:44

    Assuming you have:

    char string1[20];
    char string1[20];
    int intA;
    

    you could do:

    fscanf(file, "%19[^,],%19[^,],%d\n", string1, string2, &intA);
    

    %[^,] reads a string of non-comma characters and stops at the first comma. 19 is the maximum number of characters to read (assuming a buffer size of 20) so that you don't have buffer overflows.

提交回复
热议问题