• C

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.
bzzoyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bzzoyAuthor Commented:
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
0
aib_42Commented:
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.
0
bzzoyAuthor Commented:
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.
0
baboo_Commented:
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?




0
aib_42Commented:
bzzoy, it is very easy to use pointers as such. Here is a simple function that changes the first element of the array to 2 and the second to 10.

void modify(double *theArray)
{
    theArray[0] = 2;
    theArray[1] = 10;
}

Perhaps your main function would look like this:

int main(void)
{
    int numberoflines;
    double *theArray;

    numberoflines = getnumberoflines();

    theArray = malloc(numberoflines * sizeof(double));

    modify(theArray);

    /* Don't forget to free the array when you're done with it. */
    free(theArray);
    return 0;
}

Please note that I haven't done any error checking. This is just an example.

Basically, all you need to do is allocate some memory using malloc(), which will be your array. Of course, you want to store the address of the memory in some variable (theArray in above case) to be able to access the array. Whenever you need to access the array from some other function, you have to pass the address. Don't let the fact that theArray is declared as a pointer to double confuse you - it simply holds an integer, the address of the array allocated by malloc(). Feel free to pass it back and forth between functions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.