开发者

Can't separate string using sscanf?

I have a text file of associated numbers i.e;

1 2 2

3 2 1

3 4 3

Each line is a seperate piece of information, as such I am trying to read it in one line at a time and then seperate it into the 3 numbers but sscanf isn't doing what I expect it to.

char s[5];
char e[5];
char line[100];
int d;

fgets(line, sizeof(line), inFile);
sscanf(line, "%s %s %d", s, e, d);

putting in a printf after fgets yeilds:

1 2 2

but then after sscanf, the variables 's' and 'e' are null while 'd' is some random number that I can't even figure 开发者_高级运维out where it's come from.

Not sure what I'm doing wrong, any help would be greatly appreciated.


We really need to see your variable declarations, but in the case of d you should definitely be passing the address:

sscanf(line, "%s %s %d", s, e, &d);

From your comment, it seems you are not declaring the strings correctly. You want something like:

char s[10], e[10];

depending on how big you expect the strings to be. But you must specify a size. The line variable should be declared similarly.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜