queue stored in a binary file

Posted on 2004-11-20
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.
Question by:bzzoy

    Author Comment

    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
    LVL 7

    Expert Comment

    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.

    Author Comment

    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.
    LVL 3

    Expert Comment

    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?

    LVL 7

    Accepted Solution

    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));


        /* Don't forget to free the array when you're done with it. */
        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.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    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  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now