Thread: Placing #'s from a file into an array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Placing #'s from a file into an array

    Yes, another newbie.

    I'm trying to read numbers from a file and place them into an array. What I would like to know:

    In C, do you have to create the array of a specific size and then place the numbers in it?

    OR

    Can you read the file and have it create and determine the size of the array without any array declarations?

    Example of code would be nice but not necessary, I just need to know if it is possible and maybe a point in the right direction (hint).

    Thanks,

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    I would also be interested to find out if this can be done.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always just use the search button. Oh, and perhaps read the FAQ on reading from a file.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Use int*array, realloc(), free(), and fscanf().

    Something like this:
    Code:
    #include <stdlib.h>  /* for realloc(), free() */
    
    int *array, x, *p;
    size_t arraylen = 0, n;
    
    while(fscanf(fp, "%i", &x)) {
        p = realloc(array, (arraylen+1));
        if(!p) {
            free(array);
            perror("Out of memory");
            exit(1);
        }
        array = p;
    
        array[arraylen++] = x;
    }
    
    for(n = 0; n < arraylen; n ++) {
        printf("%i ", array[n]);
    }
    
    free(array);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're allocating bytes, but storing integers. You might want to fix that.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > int *array, x, *p;
    If you're calling realloc initially (rather than malloc), you need to make sure the input pointer is initialised to NULL
    int *array = NULL, x, *p;

    > while(fscanf(fp, "%i", &x))
    Try
    while(fscanf(fp, "%i", &x) == 1)
    Not only does this store only valid conversions, it also doesn't loop forever when it reaches EOF
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM