Can fscanf() read whitespace?

后端 未结 6 1743
野趣味
野趣味 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:08

    You're running up against the limits of what the *scanf family is good for. With fairly minimal changes you could try using the string-scanning modules from Dave Hanson's C Interfaces and Implementations. This stuff is a retrofit from the programming language Icon, an extremely simple and powerful string-processing language which Hanson and others worked on at Arizona. The departure from sscanf won't be too severe, and it is simpler, easier to work with, and more powerful than regular expressions. The only down side is that the code is a little hard to follow without the book—but if you do much C programming, the book is well worth having.

    0 讨论(0)
  • 2020-12-09 01:10

    The simplest thing would be to issue a

    fscanf("%*s");
    

    to discard the first part and then just call the fgets:

    fgets(str, stringSize, filePtr);
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-12-09 01:12

    If you insist on using scanf, and assuming that you want newline as a terminator, you can do this:

    scanf("%*s %[^\n]", str);
    

    Note, however, that the above, used exactly as written, is a bad idea because there's nothing to guard against str being overflown (as scanf doesn't know its size). You can, of course, set a predefined maximum size, and specify that, but then your program may not work correctly on some valid input.

    If the size of the line, as defined by input format, isn't limited, then your only practical option is to use fgetc to read data char by char, periodically reallocating the buffer as you go. If you do that, then modifying it to drop all read chars until the first whitespace is fairly trivial.

    0 讨论(0)
  • 2020-12-09 01:17

    A %s specifier in fscanf skips any whitespace on the input, then reads a string of non-whitespace characters up to and not including the next whitespace character.

    If you want to read up to a newline, you can use %[^\n] as a specifier. In addition, a ' ' in the format string will skip whitespace on the input. So if you use

    fscanf("%*s %[^\n]", &str);
    

    it will read the first thing on the line up to the first whitespace ("title:" in your case), and throw it away, then will read whitespace chars and throw them away, then will read all chars up to a newline into str, which sounds like what you want.

    Be careful that str doesn't overflow -- you might want to use

    fscanf("%*s %100[^\n]", &str)
    

    to limit the maximum string length you'll read (100 characters, not counting a terminating NUL here).

    0 讨论(0)
  • 2020-12-09 01:21

    I highly suggest you stop using fscanf() and start using fgets() (which reads a whole line) and then parse the line that has been read.

    This will allow you considerably more freedom in regards to parsing non-exactly-formatted input.

    0 讨论(0)
提交回复
热议问题