Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading unknown number of values from a binary file

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US
I need to read 'short' values from a file into an array. The problem is I do not know how many values are present in the file. How do I dynamically increase the size of an array in C?
 
Dyanmically allocate memory with malloc (a reasonable amount) and set a cappacity variable equal to that ammount. When the array gets full, use realloc to aquire more (typically you'll want double the initial size) and increase your capcity variable to the new size.

[plug=shameless]
[/plug]
 
read the size of the file and allocate the file size as an array, and read in ?

If a huge file, then use a buffered approach, reading in, for example 4096 bytes at a time untill the whole file is read.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Reading the file size in is a good idea, except I have to declare the array before I can do any calculations. Don't I?

int my_arr[??];

The ?? is what I come to know only after reading the file. malloc and realloc sound like a better idea, but this would make the code a bit tricky to understand. My 'customer' wants the simplest code so that they can understand/change it later on. They are Biomedical engineers and do not know too much C.

Any other suggestions?
 
Not really, you use pointers.
You could declare a pointer to ints like this:
Code:
int number_of_ints = 27; /* Could be read from file or calculated */
int *my_ints = malloc(number_of_ints * sizeof(int));
if(! my_ints) { /* Handle malloc failure error here */ }
/* Now my_ints points to 27 ints of storage space */
You should check any indexing to ensure it is less than (in my example) number_of_ints.



Trojan.
 
There are functions to get the size of a file... I know there has to be a function that just returns the size, but you can use these two functions to do it also.
int fseek(FILE* stream, long offset, int origin);
Sets file position for stream stream. For a binary file, position is set to offset characters from origin, which may be SEEK_SET (beginning), SEEK_CUR(current position) or SEEK_END (end-of-file); for a text stream, offset must be zero or a value returned by ftell (in which case origin must be SEEK_SET). Returns non-zero on error.
long ftell(FILE* stream);
Returns current file position for stream stream, or -1L on error.
like so:
Code:
//without error checking
int size = 0;
int file = fopen(...);
fseek(file,SEEK_END); //go to the end
size = ftell(file);   //get current position
fseek(file,SEEK_SET); //go back to the begining

[plug=shameless]
[/plug]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top