how to read a string from a \n delimited file

后端 未结 5 458
天涯浪人
天涯浪人 2021-01-18 10:20

I\'m trying to read a return delimited file. full of phrases.

I\'m trying to put each phrase into a string.

The problem is that when I try to read the file w

5条回答
  •  旧巷少年郎
    2021-01-18 11:00

    fscanf can be modified to read past spaces. The details are a bit complicated. Here is what the man page says about %[...]

    Matches a nonempty sequence of characters from the specified set of accepted characters; the next pointer must be a pointer to char, and there must be enough room for all the characters in the string, plus a terminating NUL character. The usual skip of leading white space is suppressed. The string is to be made up of char-acters in (or not in) a particular set; the set is defined by the characters between the open bracket [ character and a close bracket ] character. The set excludes those characters if the first character after the open bracket is a circumflex ^. To include a close bracket in the set, make it the first character after the open bracket or the circumflex; any other position will end the set. The hyphen character - is also special; when placed between two other characters, it adds all intervening characters to the set. To include a hyphen, make it the last character before the final close bracket. For instance, `[^]0-9-]' means the set ``everything except close bracket, zero through nine, and hyphen''. The string ends with the appearance of a character not in the (or, with a circumflex, in) set or when the field width runs out.

    So, %[^\n] should read everything up to the carriage return.

提交回复
热议问题