Jump to content
  • Advertisement
Sign in to follow this  
OpenGL_Guru

Reading Binary Files

This topic is 3859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all i havent read binary files in a while and need some help manipulating binary files in C++ using fstream. i dont know if my code is right so here it is.
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
 //ifstream infile;
 ifstream::pos_type size;
 char *memblock;
 fstream infile;
 
 if(infile.is_open())
 {
	cout << "open successful" << endl;
        size = (int)infile.tellg();
        cout << "size of file: " << size << endl;
   	memblock = new char [size];
	infile.seekg (0, ios::beg);
	infile.read (memblock, size);
	infile.close();
 }

 cout << "the complete file content is in memory" << endl;
 
 return 0;
}




output:
Quote:
open successful size of file: 87412000
i would like to manipulate the data as doubles(x,y,z coordinates) but unsure now that i have all the data in memory how i can access the data in decimal so that i can manipulate it, echo it out to the screen for debugging reasons or whatever. any help would be appreciated. thanks!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by OpenGL_Guru
i would like to manipulate the data as doubles(x,y,z coordinates) but unsure now that i have all the data in memory how i can access the data in decimal so that i can manipulate it, echo it out to the screen for debugging reasons or whatever. any help would be appreciated. thanks!

Type cast. You can either cast a pointer to the entire block of memory ((double *)memblock) or cast a single element at a size-appropriate offset ((double)(memblock[sizeof(double) * n])).

Obviously, the latter is nastier. Use the former.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by OpenGL_Guru
i would like to manipulate the data as doubles(x,y,z coordinates) but unsure now that i have all the data in memory how i can access the data in decimal so that i can manipulate it, echo it out to the screen for debugging reasons or whatever. any help would be appreciated. thanks!

Type cast. You can either cast a pointer to the entire block of memory ((double *)memblock) or cast a single element at a size-appropriate offset ((double)(memblock[sizeof(double) * n])).

Obviously, the latter is nastier. Use the former.


do i have to write that to a file casted before i can assign some of the values to some local arrays? the file is layed out like this:

18.805 1200.54 -0.006 0.0015 0.0054

the last 3 numbers are my x,y and z coordinates. the first 2 numbers are meaningless. just FYI there are probably 10,000 of these lines.

so anyway would you be referring to something like:

infile.read(reinterpret_cast<double*>(&memblock), sizeof(double)); ??

Share this post


Link to post
Share on other sites
Quote:
the file is layed out like this:

18.805 1200.54 -0.006 0.0015 0.0054

That's not binary.

Anyway, if you have a bunch of doubles in binary format in a file (I don't know about how different processors can screw this up), you must read groups of sizeof(double) bytes and cast them, group by group, to doubles. Or simply pass a casted double* to char*. More like:

double var;
infile.read(reinterpret_cast<char*>(&var),sizeof(double));

Share this post


Link to post
Share on other sites
Quote:
Original post by Jaiminho
Quote:
the file is layed out like this:

18.805 1200.54 -0.006 0.0015 0.0054

That's not binary.

Anyway, if you have a bunch of doubles in binary format in a file (I don't know about how different processors can screw this up), you must read groups of sizeof(double) bytes and cast them, group by group, to doubles. Or simply pass a casted double* to char*. More like:

double var;
infile.read(reinterpret_cast<char*>(&var),sizeof(double));


well the actual file is not like that but i was giving you an example of the data that was in the file after either converting the file to a text file or casting it somehow. thanks for your input i am going to play around with this for a while.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!