开发者

Why fscanf not working as expected for subsequent calls?

开发者 https://www.devze.com 2023-04-04 01:05 出处:网络
char s[20]; fscanf(stream, \"%s\", s); I found that it can only work for the first time, subsequent calls will fail.
char s[20];
fscanf(stream, "%s", s);

I found that it can only work for the first time, subsequent calls will fail.

(gdb) p fscanf(stream, "%s", s)
$15 = 1
(gdb) p开发者_StackOverflow (char *)s
$17 = 0x7fffffffe770 ""

From the above can see that fscanf matches 1 character, but not stored to s?

UPDATE

Is there a way to get corresponding file name of FILE*?

file content:

a
abc
b
c
e
f
h
hi


fscanf et al return the number of items converted, not the number of bytes or characters. So you are successfully converting one item, a string in this case, which happens to be empty ("").

0

精彩评论

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