read data from binary file

Posted on 2011-04-25
Last Modified: 2012-05-11
I'm very beginner if we talk about C++.
I will to know how read data from binary file from specifically position.
I was trying:
 and when data are in memory how can I get double data array from it?

Let say I need to read some information from the binary file.
I know that this information saved as double and starts from byte number 10.
How to do that?
I know that i VB it is like this:
        Dim a as double
        Dim IndF As Integer = FreeFile()
        FileOpen(IndF, OpenFilePath, OpenMode.Binary, OpenAccess.Read)
        FileGet(IndF, a, 10)

I hope it is very easy question.
Question by:mastiSoft
    LVL 86

    Accepted Solution

    That will depend on how 'memblock' is declared - assuming that it is a char array, you could
    #define MAX_SIZE (10 * sizeof(double)) // space for 10 double values
    char memblock[MAX_SIZE];
    size_t size = MAX_SIZE;
    double* pd = (double*) memblock; // cast to 'double*', can be used as an array
    for (size_t i = 0; i < MAX_SIZE; ++i) {
      cout << pd[i] << endl;

    Open in new window

    LVL 7

    Expert Comment

    If you already have an array of double, cast to char* instead of the other way around.

    double buffer[50]; ((char*)buffer, sizeof(buffer));

    To do it dynamically with STL vector, it would look like this:

    vector<double> buf;
    buf.resize(50);[0], sizeof(double)*buf.length());
    LVL 1

    Author Closing Comment

    Thank you. It works fine.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    745 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

    14 Experts available now in Live!

    Get 1:1 Help Now