views:

237

answers:

1

Hi, 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.

+3  A: 

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.

anon
ok fair enough, that's a dumb mistake.i've tried declaring s and e as char s, char e or as char s[], char e[], neither seems to do the trick.
TeeJay
@Trent How about char s[10], e[10];
anon
yeah, tried, no luck. Seg fault actually.
TeeJay
@Trent Well, you need to post more code. Please do this by editing your original question.
anon
ok scratch that, i think i have it sorted now, forgot to add in the address of d.Thanks Neil
TeeJay