Can fscanf() read whitespace?

后端 未结 6 1742
野趣味
野趣味 2020-12-09 00:29

I\'ve already got some code to read a text file using fscanf(), and now I need it modified so that fields that were previously whitespace-free need to allow whi

6条回答
  •  囚心锁ツ
    2020-12-09 01:11

    If you cannot use fgets() use the %[ conversion specifier (with the "exclude option"):

    char buf[100];
    fscanf(stdin, "%*s %99[^\n]", buf);
    printf("value read: [%s]\n", buf);
    

    But fgets() is way better.


    Edit: version with fgets() + sscanf()

    char buf[100], title[100];
    fgets(buf, sizeof buf, stdin); /* expect string like "title: TITLE WITH SPACES" */
    sscanf(buf, "%*s %99[^\n]", title);
    

提交回复
热议问题