开发者

using scanf and family to read in two strings separated by a space from a file in c

I am trying to read in 2 string that are separated by a space from a file.

Whatever I try I keep getting the 1st string initialized but the second string is always NULL.

Some of the formatters that I have tried are "%s%s" , "%s %s" , "%s[\n\t ]%s"

Any ideas of what I am do开发者_运维技巧ing wrong?

I think it has to do with the internal buffer of scanf -- reads the first %s then puts some invisible character in buffer reads that with 2nd %s which then is read and the second string is NULL when complete.


What do your strings look like?

I don't think your hypothesis of fscanf() modifying the input data by placing "some invisible character in buffer" is true.

It seems more likely that your strings don't conform to the requirements of the %s format specifier.


between your string, if there is just one space

fscanf ( ..., "%s %s", ... ) ; // you know how to fill space marked with ...

but if number of white space is not known :

char stack[YourscreenSize];
fscanf ( ..., "YourscreenSize[^\n]", stack );  // take all line in one data, 

then parse it

if number of white space is not known, ( second way )

  • take data from file
  • check it, send first char in isgraph function whether it is white space or not
  • if it is white space, then erase stored data

do it, iteratively.When you see EOF break from iteration, (you can chech return value of fscanf to know whether it reads char or EOF )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜