How would you scan the values that are in a .txt file and store the values into an array?
How would you scan the values that are in a .txt file and store the values into an array?
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.
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?
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?".
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
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.
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.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; }
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.
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.
Ok this is what I have so far, let me know if I'm heading in the right direction at all:
Now everytime I run it my program closes because it "Stops Working".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]; }
Last edited by skmightymouse; 04-28-2012 at 12:32 PM.
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.
It's the latter? I'm not sure if I know what that means.
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
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?
I think it's the former!
The brackets are such that the third argument to fscanf isCode:while (fscanf(ifp, "%i", &array[i] !=0))
Instead of comparing the result of fscanf. Adak is right though -- comparing to 0 isn't what you want to do.Code:&array[i] != 0