Scanning txt file values into an array

This is a discussion on Scanning txt file values into an array within the C Programming forums, part of the General Programming Boards category; How would you scan the values that are in a .txt file and store the values into an array?...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    44

    Scanning txt file values into an array

    How would you scan the values that are in a .txt file and store the values into an array?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    If the number of values is not fixed, you'll need to make use of dynamic allocation on a 2D array of pointers to the values. Also, depending on how to plan to keep them in memory will have an effect. Do you plan to read them as text, ints, floats, etc.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    I think I plan to read them in as ints.

    Can you give me an example of how I would write code to do that?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    How you read in data ALL depends on how the data is formatted, AND what parts of the data you want to get from the file, as well as how you need to work with it, etc.

    The first question is "What's the arrangement of the data, in the file?".

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by Adak View Post
    The first question is "What's the arrangement of the data, in the file?".
    This is pretty crucial.

    Is it one value/line? Multiple values on a line? Is there even lines at all or is it one long block?

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    It is just a file that contains values like this:

    55
    336
    56
    886
    112
    20

    Like that and then stores those values to an array

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A naive but simple way is to use a fixed array size and just fscanf the file in a loop. But then you also need to keep track of the amount of read numbers. You can use a for loop to do both. fscanf returns the amount of items read, so the condition to check for is 1. The for loop has facilities to keep track of the amount.

    Code:
        for(i = 0; fscanf(fp, "%d", &a) == 1; i++) {
            if( i == ARRAYSIZE ) {
                fprintf(stderr, "File content too large\n");
                fclose(fp);
                return 1;
            }
            array[i] = a;
        }
    There are some problems with this obviously, but it's a place to start. If you want to be able to handle any size, and deal with unexpected input from the file you need to take a different approach.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Ya my array has the possibility to hold up to 1500 values are there aren't always going to be that many values so I need it to adapt. Sorry I would have said something if I knew that the approach would have been different.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    1,500 int's is not an outrageous amount to memory to use. Then put in your values, whether you have 5 or 1,500. You are counting them into the array (at their index locations), anyway, so it's no extra work to keep a count of the number of int's you're working with.

    You could count them first, then rewind() in the file, create the array of the perfect size, and then load the data into the array. For normal use, with 1,500 int's, I might not bother though.

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Ok this is what I have so far, let me know if I'm heading in the right direction at all:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FILENAME "C:/mydata.txt"
    
    int read_file (FILE *ifp);
    
    int main (void)
    {
        int array [1500];
    
        FILE *ifp;
    
        ifp = fopen ("C:/mydata.txt", "r");
    
        read_file(ifp);
    
        return 0;
    }
    
    int read_file (FILE *ifp)
    {
        int i = 0;
        int array[1500];
        
        while (fscanf(ifp, "%i", &array[i] !=0))
        {
            printf ("%i\n", array[i]);
            i++;
        }
        return array[i];
    }
    Now everytime I run it my program closes because it "Stops Working".
    Last edited by skmightymouse; 04-28-2012 at 01:32 PM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're on the right track.

    Change the != 0 to > 0 in the while test.

    "Stops working" usually means your program has tried to access outside it's memory or array boundaries. In this case, it's the latter.

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    It's the latter? I'm not sure if I know what that means.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Learn your language, maybe?

    Showing results for definition of latter
    Search instead for definition of "latter
    Search Results

    latĚter/ˈlatər/
    Adjective:
    Situated or occurring nearer to the end of something than to the beginning.
    Belonging to the final stages of something.
    Synonyms:
    last - final - late - ultimate - recent
    More info ╗Dictionary.com - Answers.com - Merriam-Webster - The Free Dictionary

  14. #14
    Registered User
    Join Date
    Mar 2012
    Posts
    44
    Sorry I'm not trying to be that guy that doesn't google and research stuff before posting but I searched latter and I didn't get any valid results so I just thought that you might have misspelled it or something.

    So should I allocate my array down to the amount of items actually read that way it doesn't try and operate outside of its boundaries?

  15. #15
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    I think it's the former!

    Code:
     while (fscanf(ifp, "%i", &array[i] !=0))
    The brackets are such that the third argument to fscanf is
    Code:
    &array[i] != 0
    Instead of comparing the result of fscanf. Adak is right though -- comparing to 0 isn't what you want to do.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-25-2012, 05:30 AM
  2. Sending Values and Scanning an Array
    By xxxixpats in forum C Programming
    Replies: 7
    Last Post: 11-06-2010, 12:32 AM
  3. Replies: 12
    Last Post: 09-23-2010, 12:49 PM
  4. Replies: 1
    Last Post: 04-25-2006, 01:14 AM
  5. assigning values in file to an array
    By divinyl in forum C++ Programming
    Replies: 9
    Last Post: 07-29-2003, 09:33 AM

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