Thread: Reading from stdin and printing (Segmentation Fault)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Reading from stdin and printing (Segmentation Fault)

    I'm writing a program that reads from stdin and prints it back out. I'm getting a bunch of garbage then a seg fault.

    Code:
       double numbers[256];
       memset (numbers, 0, sizeof (numbers));
       for (;;) {
          double num = scanf ("%255lg", numbers);
          if (num== EOF) 
             break;
       }
       for (int i = 0; i < (int)sizeof (numbers); ++i) {
          printf ("%g\n", numbers[i]);
       }
    Last edited by auxfire; 03-08-2013 at 03:34 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The format specifier %255lg tells scanf() to read a single floating point value, which might have a width (in terms of characters typed in) of 255 characters.

    scanf() does not provide any format specifier for reading a whole array of floating point values.

    Try reading one value at a time, but do that in a loop 256 times.

    Also, scanf() returns an integer, not a double. The return value is not always EOF when an error occurs.

    So, try looking more closely at the documentation for scanf().


    sizeof(numbers) also does not yield the number of values in the array. Nor does it have any relationships to the number of values read in. In fact sizeof(numbers) in your case will probably be 2048 (8 times 256).
    Last edited by grumpy; 03-08-2013 at 03:24 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70
    I'm trying to start reading code other than my own....so at first glance after your post grumpy....what about the second line? Shouldn't that "buffer" say "numbers" instead? It looks to me to be the intended operation, and I don't see where buffer is defined.

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by Sorin View Post
    I'm trying to start reading code other than my own....so at first glance after your post grumpy....what about the second line? Shouldn't that "buffer" say "numbers" instead? It looks to me to be the intended operation, and I don't see where buffer is defined.
    I was doing some test. Buffer in memset is suppose to be numbers. And thanks grumpy I realized it's an array of 1000 doubles. I was used to creating char pointers and never really created an array this way before.

    Code:
       double numbers[1000];
       memset (numbers, 0, sizeof (numbers));
       int count = 0;
       for (int i = 0; ; ++i) {
          int return_value = scanf ("%lg", &numbers[i]);
          if (return_value == EOF) break;
          ++count;
       }
       for (int i = 0; i < count; ++i) {
          printf ("%g\n", numbers[i]);
       }
    Last edited by auxfire; 03-08-2013 at 03:38 AM. Reason: Added revised code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault reading and parsing data from a text file
    By deathseeker25 in forum C Programming
    Replies: 4
    Last Post: 05-19-2012, 12:33 PM
  2. Segmentation fault when reading very large text files
    By sapogo06 in forum C Programming
    Replies: 8
    Last Post: 12-07-2009, 03:19 PM
  3. Segmentation fault reading integers into an array
    By bolivartech in forum C Programming
    Replies: 4
    Last Post: 10-23-2009, 08:06 PM
  4. Replies: 3
    Last Post: 04-19-2004, 06:42 PM
  5. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM