[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

queue stored in a binary file

Posted on 2004-11-20
5
Medium Priority
?
201 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:bzzoy
  • 2
  • 2
5 Comments
 

Author Comment

by:bzzoy
ID: 12635730
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
 
LVL 7

Expert Comment

by:aib_42
ID: 12636070
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
 

Author Comment

by:bzzoy
ID: 12636114
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
 
LVL 3

Expert Comment

by:baboo_
ID: 12636997
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
 
LVL 7

Accepted Solution

by:
aib_42 earned 150 total points
ID: 12637762
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question