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:
From your comment, it seems you are not declaring the strings correctly. You want something like:
depending on how big you expect the strings to be. But you must specify a size. The
linevariable should be declared similarly.