Question:
File into array of unknown size in c?
Joe Shmoe
2010-08-23 10:10:11 UTC
In C I can create an array like this:

char foo[][32] = {"one", "two", "three", "four"};

and I can access each element of the array like this:

foo[x]

How can I read a text file of unknown size and access each word like this?
Five answers:
Cubbi
2010-08-23 10:26:52 UTC
You can allocate the array using malloc() with some reasonable size, and grow the array calling realloc(), if the next entry read from file would not otherwise fit.



Edit: Me M elaborated well about array capacity vs. the number of elements actually stored.

Note also that since each element of your particular array is itself a pointer to an array (of char), those arrays of char will have to be allocated as well. If those strings can be arbitrary long as well, the best solution in C is a character-by-character loop.
schecter
2016-12-15 19:47:31 UTC
Read File Into Array C
MichaelInScarborough
2010-08-25 04:45:28 UTC
After so many guesses, here is the right thing to do.

Count the number of words and the size of the biggest word, before sizing and populating the array in order to avoid realloc calls.

Allocate the char ** array and the "biggest word" buffer.

fseek to the start of the file

use a for loop to read the words using fgetc into the "biggest word" buffer

allocate necessary memory and copy it to the current array element



naturally the above instruction assume that the text file is static and you can get an exclusive lock on it during execution of your program.
Me M
2010-08-23 10:40:25 UTC
Cubbi's answer is good, I just want to add a couple suggestions.



When you are dynamically allocating an array, you may want to keep an additional variable to keep track of how large your array is.... if you use malloc or realloc to allow your array to hold x number of items, then store x in a variable so you know if you need to use realloc again because you have too many items.



Depending on what you are doing and how often you are going to be increasing the size of the array, you may want to increase the size of the array by many items at a time (instead of just allowing it to hold one more item each time you realloc). This is because reallocating memory can take more time than other program instructions/calculations and it will allow your program to run more efficiently by limiting the time wasted on memory allocation. If you do this, you will also need another variable to track how many items are actually in your array and not just how many items your array can hold.
Anonymous
2010-08-24 20:49:27 UTC
You must first find the size of the file:

http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c



Then allocate memory for storing it using "malloc()".

Then copy the file into the allocated memory.



Look here:

http://www.anyexample.com/programming/c/how_to_load_file_into_memory_using_plain_ansi_c_language.xml


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...