# Thread: Problem with fscanf statement

1. ## Problem with fscanf statement

Hi this is my first time doing this, but I am having trouble with my fscanf statement.
The text file that I am trying to read looks like this:

0 1 1
1 1 1
2 1 1
3 1 1
4 1 1
5 1 1
6 1 1
7 1 1
8 1 1
9 1 1

and I am trying to read the first number in each line as "i" and the second one as what x[i] equals and the third as what y[i] equals.

here is the fscanf statement i'm using right now:

inp = fopen ("input-xy.txt", "r");

input_status = fscanf(inp, "%d %f %f", &i, &x[i], &y[i]);

but when i try to print these numbers out, I get a segmentation fault.

Any ideas?

-----Julian

2. I can guarantee that the computer will not read i before trying to figure out where x[i] is. You'll have to do it in two chunks, to make sure you get i, first, then get x[i] and y[i], later.

3. what do you mean by two chunks? Like two fscanf statements?

4. Right.

5. ok so I did it like this:

input_status = fscanf(inp, "&#37;d", &i);
input_status2 = fscanf(inp, "%f %f", &x[i], &y[i]);

and now I don't get a segmentation error but it prints out

0 0.007813 0.007813

0 1 1 like it should.

What's the problem now?
thanks for helping with the error!

----julian

6. Coincidentally, the bit pattern of 1.0f (as a float), when interpreted as a double and padded with zeros (on the right), is 0.0078125. If x and y are supposed to be doubles, then you need to use &#37;lf in your fscanf.

7. thanks! Yeah I should have known that. Now it works great! Thank you so much!

---Julian

8. actually i have one more question. How do I program it to keep scanning until the end of the file. I know I use EOF but when i put in

while(input_status != EOF && input_status2 != EOF)

it doesn't stop and does it in a loop.

How do i just go to the end of the file taking in the first number as i, the second as x[i] and the third as y[i]?

9. fscanf returns the number of items read. So when you try to read i, and you read 0 items....

10. I'm confused.

Here is my whole program. Maybe you can see why i keep getting a segmentation error.

insert
Code:
```#include <stdio.h>

int min_number (int x, int y)
{
if (x <= y) return x;
else return y;
}
int max_number (int x, int y);
{
if (x <= y) return y;
else return x;
}

main()
{
FILE *inp;

int a, b, c, k, j, i, input_status;
double x[10];
double y[10];
double temp[j];
double temp2[j];
double z[19];
int max_number(int x, int y);
int min_number(int x, int y);

inp = fopen ("input-xy.txt", "r");

input_status = fscanf(inp, "&#37;d %lf %lf", &a, &b, &c);

while(input_status != EOF)
{
x[a] = b;
y[a] = c;
/*
for (j = 0; j <= 18; j++)
{
for(k = max_number((j-9), 0); k<= min_number(9, j); k++)
{
z[j] += x[k]*y[j-k];
}
printf("array z[%d] is %f", j, z[j]);
printf("\n");
}
*/
printf("%d %lf %lf", a, b, c);
}
input_status = fscanf(inp, "%d %lf %lf", &a, &b, &c);
fclose(inp);

}```

11. What happened to the two different fscanf statements?

12. My teacher told me to do it like that. And I also did it with the two fscanf statements again and i got a segmentation fault. Its like anyway I do it I get a segmentation fault.

----Julian

13. Well, you can't write a %lf into b, if it's an int. And you need to have the fscanf inside the loop, not outside it.

14. Yeah I found that error but after I changed it, I still get a segmentation error. Any ideas?

15. Code:
```#include <stdio.h>

int min_number (int x, int y)
{
if (x <= y) return x;
else return y;
}
int max_number (int x, int y) /*Note no semicolon here*/
{
if (x <= y) return y;
else return x;
}

int main()
{
FILE *inp;

int a, k, j, i, input_status;
double b, c; /* needs to be double */
double x[10];
double y[10];
/*double temp[j];
double temp2[j]; */ /* commented out 'cause it's not like it would compile anyway */
double z[19];
int max_number(int x, int y);
int min_number(int x, int y);

inp = fopen ("input-xy.txt", "r");

input_status = fscanf(inp, "%d %lf %lf", &a, &b, &c);

while(input_status != EOF)
{
x[a] = b;
y[a] = c;
/*
for (j = 0; j <= 18; j++)
{
for(k = max_number((j-9), 0); k<= min_number(9, j); k++)
{
z[j] += x[k]*y[j-k];
}
printf("array z[%d] is %f", j, z[j]);
printf("\n");
}
*/
printf("%d %lf %lf", a, b, c);
input_status = fscanf(inp, "%d %lf %lf", &a, &b, &c); /* moved to inside the loop */
}
fclose(inp);

}```
What you had posted wouldn't have compiled, so there's no way you were running it.