By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,131 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,131 IT Pros & Developers. It's quick & easy.

Exporting Data in Binary Format

emaghero
P: 85
Greetings one and all,

I wish to output data to a file in binary format. The data will take the form of a 2-D array of doubles. Calculated in C++. It (the file) will be used in conjunction with a program which does contour plots.

I have an inkling that this can be done in Microsoft Visual Studio using the fstream class and the insertion operator, as you would for a normal txt file.

Can anybody help me out or poitn me in the right direction?
Mar 20 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510

emaghero
P: 85
The following code tells you how to create a binary file in C++. Also give is an example of how to export to a binary file.

The argument that can be given to a fstream object ios_base::binary is strictly speaking not a binary file. Check it with a Hex Editor and you'll see it's still in txt format.

The following section of code works in Microsoft Visual Studio, but as far as I'm aware is not Microsift specific.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <tchar.h>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <cmath>
  7. //The following headers are required for binary files
  8. #include <fcntl.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <io.h>
  12. #include <stdio.h>
  13.  
  14.  
  15.  
  16. using namespace std;
  17.  
  18. int _tmain(int argc, _TCHAR* argv[]){
  19.  
  20.     int bytes;//This is a variable that contains the data that is being sent to the binary file
  21.  
  22.     int file=_open("c:\\Modes.pm",_O_BINARY|_O_WRONLY|_O_CREAT,_S_IREAD|_S_IWRITE);//Command to open a binary file for reading and writing
  23.  
  24.     if(file==-1){
  25.         perror("Failed to open file");//Error message in case the file is not created
  26.     }
  27.     else{
  28.         cout<<"file is open\n";
  29.     }
  30.  
  31.     char *header="Robert's File Format";//Create a pointer to a header
  32.  
  33.     bytes=_write(file,header,(unsigned int)(strlen(header)));//This writes the header to the binary file
  34.  
  35.     int revision=1;//Revision number for the file format
  36.  
  37.     bytes=_write(file,(void *)&revision,sizeof(int));//This sends the revision number to the binary file
  38.  
  39.     int num_modes=3;//This tells the user how many modes are inside the binary file
  40.  
  41.     bytes=_write(file,(void *)&num_modes,sizeof(int));//This sends the number of modes to the binary file
  42.  
  43.     //Does the void pointer mean that it doesn't matter what type of data you're 
  44.     //sending to the file as long as the size is correct? It does matter what the type is.
  45.  
  46.     int row = 6;//The number of rows that the array will have
  47.     double col = 5;//The number of columns that the array will have
  48.  
  49.     //Send this data to the binary file
  50.     bytes=_write(file,(void *)&row,sizeof(int));
  51.     bytes=_write(file,(void *)&col,sizeof(int));
  52.  
  53.     //Doubles can also be sent to the binary files
  54.     double d;
  55.     //Set up a loop over the modes
  56.     for(int k=0;k<num_modes;k++){
  57.         for(int i=0;i<row;i++){
  58.             for(int j=0;j<col;j++){
  59.                      d=cos((double)(j))*cos((double)(j))*sin((double)(i))*sin((double)(i));
  60.                 bytes=_write(file,(void *)&d,sizeof(double));
  61.             }
  62.         }
  63.     }
  64.     cout<<"Data successfully exported\n";
  65.     _close(file);//Close the file when you are finished operating with it
  66.  
  67.     system("PAUSE");
  68.     return 0;
  69. }
  70.  
  71.  
There is a Microsoft specific class that enables the user to to use the insertion operator to write to a binary file. However, since I want my code to be platform independent I have not learned much about it.
Mar 27 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.