We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

read data from binary file

Medium Priority
445 Views
Last Modified: 2012-05-11
Hi,
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:
file.seekg(0,ios::beg);
file.read(memblock,size);
 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)
        FileClose(IndF)

I hope it is very easy question.
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2012
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
If you already have an array of double, cast to char* instead of the other way around.

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

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

vector<double> buf;
buf.resize(50);
file.read(&buffer[0], sizeof(double)*buf.length());

Author

Commented:
Thank you. It works fine.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.