Problem with fscanf statement

This is a discussion on Problem with fscanf statement within the C Programming forums, part of the General Programming Boards category; Hi this is my first time doing this, but I am having trouble with my fscanf statement. The text file ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    27

    Question 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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    what do you mean by two chunks? Like two fscanf statements?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Right.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    ok so I did it like this:

    input_status = fscanf(inp, "%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

    instead of

    0 1 1 like it should.

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

    ----julian

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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 %lf in your fscanf.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    thanks! Yeah I should have known that. Now it works great! Thank you so much!

    ---Julian

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    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. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    fscanf returns the number of items read. So when you try to read i, and you read 0 items....

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    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. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What happened to the two different fscanf statements?

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    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. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    Yeah I found that error but after I changed it, I still get a segmentation error. Any ideas?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 05:21 PM
  2. Multithreading problem
    By Bacardi34 in forum C Programming
    Replies: 5
    Last Post: 09-02-2004, 03:26 PM
  3. Replies: 1
    Last Post: 08-31-2004, 05:07 AM
  4. problem using strtok and fscanf together
    By osal in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 03:21 PM
  5. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 05:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21