Thread: Reading / input column of numbers into an array

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    31

    Reading / input column of numbers into an array

    Hello all.

    I have a text file with one column of numbers (vector) e.g.:

    1
    2
    3
    4
    5
    6

    They are separated by a return.

    As I'm fairly new to C I thought I would check out my logic with you before I start writing the code. So the sequence of events to read in a vector should be:

    1. Use fopen and the read option to get the file open.
    2. Use scanf to make a string containing the contence of a line. It will know when to stop when it finds \n.
    3. Convert the string of charaters in 2. into a number.
    4. Put the number into an elemet of an array
    5. Loop 2. -> 4. until EOF is returned
    4. fclose
    5. return number array.

    If you have experience with this kind of thing, how does that sound? Any advice?

    Cheers, Daniel.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by boyfarrell
    2. Use scanf to make a string containing the contence of a line. It will know when to stop when it finds \n.
    3. Convert the string of charaters in 2. into a number.
    4. Put the number into an elemet of an array
    5. Loop 2. -> 4. until EOF is returned
    I might use fscanf to read and convert until it fails, putting a successfully converted value directly into the array.
    Code:
    #include <stdio.h>
    
    size_t foo(const char *filename, int *array, size_t size)
    {
       size_t count = 0;
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          int *end = array + size;
          while ( fscanf(file, "%d", array) == 1 )
          {
             if ( ++array >= end )
             {
                puts("too many inputs");
                break;
             }
             ++count;
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return count;
    }
    
    int main()
    {
       int array[10];
       size_t i, count = foo("file.txt", array, sizeof array / sizeof *array);
       for ( i = 0; i < count; ++i )
       {
          printf("array[%lu] = %d\n", (long unsigned)i, array[i]);
       }
       return 0;
    }
    
    /* my output
    array[0] = 1
    array[1] = 2
    array[2] = 3
    array[3] = 4
    array[4] = 5
    array[5] = 6
    */
    Last edited by Dave_Sinkula; 08-25-2005 at 08:51 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    Hey thanks so much for the code! It works a perfectly.

    I will expand it to be able to read in files that have more than one column, i.e. separated my a tab or a space etc. So , don't post any code I want to do this my self!!! . Will post up when I'm finished.

    Daniel.

    PS - by the way where did you get that code from, you had it lying around or pulled it off the net? Thanks again.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    207
    Dave's a good programmer, so he probably made it up. He's helped me too.

    mw
    Blucast Corporation

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    Think I will be very easy to make fscanf read a file with 2 columns. You just need to change the 'format' (i.e. second input of fscanf) to \f\t\f\n. This would expect a float, a tab, another float and then a new line.

    I forgot to mention. I'm having some trouble making this code accept doubles? I have changed it a few things around:
    Code:
    #include <stdio.h>
    
    int foo(const char *filename, double *array, int size)
    {
       int count = 0;
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          double *end = array + size;
          while ( fscanf(file, "%f", array) == 1 )
          {
             if ( ++array >= end )
             {
                puts("too many inputs");
                break;
             }
             ++count;
          }
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return count;
    }
    
    int main()
    {
       double array[10];
       int i, count;
       count = foo("test1.txt", array, sizeof array / sizeof *array);
       for ( i = 0; i < count; ++i )
       {
          printf("array[%i] = %d\n", (int)i, array[i]);
       }
       return 0;
    }
    This builds with out any error, but returns junk! Have a look:
    Code:
    array[0] = 1065353216
    array[1] = 1073741824
    array[2] = 1079613850
    array[3] = 1085485875
    array[4] = 1091148186
    array[5] = 1092301619
    I have changed size_t because I don't really understand it. But I figure the output of 'foo' will always be an int. Also, the input will always be a double. Can anybody see the problem? (I have of course changed the input file to contain numbers that have a decimal point):
    1.0
    2.0
    3.4
    5.6
    8.6
    9.7

    Sorry about this guys but the printf should read
    Code:
     printf("array[%i] = %f\n", (int)i, array[i]);
    This also changes the output as would be expected. However, it's still not what's in the file!
    Last edited by boyfarrell; 08-28-2005 at 06:20 AM.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while ( fscanf(file, "%f", array) == 1 )
    The conversion for a double is "%lf"

    If you're using gcc as your compiler, use the
    -W -Wall
    options as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    You legend!

    Thanks so much, works perfectly now.

    Cheers, Daniel.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. entering numbers to array in a row whithout length input
    By transgalactic2 in forum C Programming
    Replies: 55
    Last Post: 01-02-2009, 04:02 AM
  2. trying to read input into an array
    By trprince in forum C Programming
    Replies: 16
    Last Post: 11-17-2007, 06:20 PM
  3. arrays
    By john_murphy69 in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:33 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 07:23 PM