473,382 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Exporting Data in Binary Format

emaghero
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
2 3504
horace1
1,510 Expert 1GB
have a look at binary files in this tutorial
http://www.cplusplus.com/doc/tutorial/files.html
http://www.cprogramming.com/tutorial/lesson10.html
Mar 20 '07 #2
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

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

Similar topics

4
by: D | last post by:
I've created a report with many subreports of aggregate data. I want my client to be able to export this data to Excel to make her charts, etc. Only one problem: one of the fields is a "SchoolYear"...
5
by: mik18 | last post by:
I'm having trouble with exporting reports to Word in the rtf format and I'm hoping someone has a solution. The reports are losing their formats. Not all the formating is lost but some is and of...
8
by: Jerry | last post by:
I have an off-the-shelf app that uses an Access database as its backend. One of the tables contains a field with an "OLE Object" datatype. I'm writing some reports against this database, and I...
3
by: nigel.thomson | last post by:
Hello All Is there an easy way to do this? I have a database that contains records witha image as one of the fields, what I want to do is export the images to a seperate folder, in whatever...
6
by: sara | last post by:
I have what I think is a little strange...I have to get data from our payroll system into a specific format (fixed record length) as a .txt or .prn file only to upload to our 401k custodian. I...
0
by: =?Utf-8?B?ZGVuIDIwMDU=?= | last post by:
hi, I trying to export data display on a gridview that supports any language (like chinese, japanese, thai, french) shown here is chinese only. There is no problem exporting english language...
3
by: nigel | last post by:
Hi, I'm using VBA to export data from a table direct to a CSV file DoCmd.TransferText acExportDelim, , "ExportTable", filePath this produced a file with COMMA separated values,...
2
by: billelev | last post by:
Hi there, My question does not concern the exporting of data to CSV so much as the format of the exported data. The data to be exported consists of a time series of prices for a number of assets....
5
by: Doogie | last post by:
Can anoyne tell me why this VBScript will create the file to Excel just fine, but the Excel file will not open up? I am saving it as a xlsx file instead of an xls one and I have the new version of...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.