Link to home
Start Free TrialLog in
Avatar of bzzoy
bzzoy

asked on

queue stored in a binary file

im working on this program that has me store a queue of doubles in a binary file, (saved using fread and fwrite), and im wondering how to load the data in the file back into a queue (im not sure if i use an array or how the buffer can be accessed).  basically what kind of structure (array, etc.) would i use to hold the queue.
Avatar of bzzoy
bzzoy

ASKER

another thing, i wont know the number of entries in the file, so im assuming i'll need to use some dynamic memory allocaiton on an array
If you're going to use dynamic memory allocation (your assumption is correct, if you don't know the number of entries in the file, or if you don't have a maximum number of entries there can be in the file, or the maximum number of entries there can be in the file is too great to fit in an array, you'll need to use dynamic memory) you're bound to use some combination of pointers and malloc.

Since the data you have is a simple queue, I recommend using a single block of memory and a pointer to it, in an array-like fashion:
1) Determine the number of doubles.
2) Allocate enough memory for them, store the allocated address in a pointer.
3) pointer[0] is the first double, read the first line into that. pointer[max-1] is the last one.
Avatar of bzzoy

ASKER

so basically i'll have a pointer that acts as an array? how would i be able to pass this between different functions ,ie i call a function to load, and have another function addNums, that uses that array but its not directly passed as a paramter.
You could code your load function to handle all the file handling stuff, and just return a pointer to the array you loaded.  But you'd have to pass the pointer to any function that operated on the array.  Unless you wanted to declare the array globally, you'd necessarily have to pass the pointer to any function expected to access the data.

Though, doing all this in main would be easier to code.  Is there some reason you need to decompose this problem?  What's the application?




ASKER CERTIFIED SOLUTION
Avatar of aib_42
aib_42

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial