I want to read floats (and ints afterwards) from a line I’m getting out of a file. When I’m debugging it, I can see it’s getting the line out of the file no problem, but when I try to sscanf it, I’m getting garbage. Here’s my code:
while(fgets(line, 1000, file) != EOF)
{
//Get the first character of the line
c = line[0];
if(c == 'v')
{
sscanf(line, "%f", &v1);
printf("%f", v1);
}
}
The value stored in v1 is garbage. Why is this not working, and how can I get floats and ints out of this line?
You’re including the first character (which is ‘v’) in the call to sscanf, so the call is failing and
v1is left untouched (with garbage in it). Try this instead: