473,395 Members | 1,680 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,395 software developers and data experts.

Simple binary file IO

Hi there,

Can anyone give me a very simple example of how to write a string of characters into a binary file?

Thanks.
Mar 9 '07 #1
2 3182
horace1
1,510 Expert 1GB
Hi there,

Can anyone give me a very simple example of how to write a string of characters into a binary file?

Thanks.
have a look at the example in
http://www.cplusplus.com/reference/clibrary/cstdio/fwrite.html
Mar 9 '07 #2
DumRat
93
Hi there,

Can anyone give me a very simple example of how to write a string of characters into a binary file?

Thanks.

You have to open the file in binary mode.

Expand|Select|Wrap|Line Numbers
  1. ofstream out;
  2. out.open(im_out, ios::out|ios::binary);
  3.  
here's something I did for my own amusement a while back.
Skip the main function.
Look for the "ofstream out" object in the functions. Hope u will get some idea.
This code is full of bugs I think.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <fstream>
  3. #include <string>
  4. #include <stdlib.h>
  5. #include <sys/stat.h>
  6. #include <math.h>
  7. #include <windows.h>
  8.  
  9. using namespace std;
  10.  
  11. void Encrypt();
  12. void Decrypt();
  13.  
  14. int main(int argc, char* argv[])
  15. {
  16.     while(true){
  17.         system("cls");
  18.         cout << "1. Encrypt" << endl;
  19.         cout << "2. Decrypt" << endl;
  20.         cout << "3. Exit" << endl;
  21.  
  22.         int choice;
  23.         cout << endl << "Enter choice : " ;
  24.  
  25.         cin >> choice;
  26.  
  27.         if(choice == 1)
  28.             Encrypt();
  29.         else if(choice == 2)
  30.             Decrypt();
  31.         else
  32.             exit(0);
  33.     }
  34.  
  35.         return 0;
  36. }
  37.  
  38. void Encrypt()
  39. {
  40.  
  41.     ifstream in_image;
  42.     ifstream in_file;
  43.     ofstream out;
  44.  
  45.     system("cls");
  46.  
  47.     char* im_in = new char[200];
  48.     char* f_in = new char[200];
  49.     char* im_out = new char[200];
  50.  
  51.     cout << "Enter input image file(.tga) : " ;
  52.     cin >> im_in;
  53.     cout << "Enter input file : " ;
  54.     cin >> f_in;
  55.     cout << "Enter output file : " ;
  56.     cin >> im_out;
  57.  
  58.     in_image.open(im_in, ios::in|ios::binary);
  59.     in_file.open(f_in, ios::in|ios::binary);
  60.     out.open(im_out, ios::out|ios::binary);
  61.  
  62. //    struct stat results;
  63. //    if(stat(f_in, &results) != 0)
  64. //    {    
  65. //        cout << "Error opening input file!" << endl;
  66. //        exit(1);
  67. //    }
  68.  
  69.     delete im_in;
  70.     delete im_out;
  71.     delete f_in;
  72.  
  73.     int i, j, k;
  74.  
  75.     char* image_header = new char[54];
  76.     char* image_buffer = new char[16];
  77.     char* file_in_buffer = new char[1];
  78.     char temp;
  79.  
  80.     int *bits = new int[8];                //Array to keep the bits in a byte.
  81.  
  82.     int operand; 
  83.  
  84.     in_image.read(image_header, 54);
  85.  
  86.     int f_length = 0;
  87.  
  88.     image_header[5] = (char)f_length%256;    
  89.     image_header[6] = (char)(f_length/256)%256;
  90.     image_header[7] = (char)(f_length/256)/256;
  91.  
  92.     out.write(image_header, 54);
  93.  
  94.     i = 0;
  95.  
  96.     do
  97.     {
  98.         in_image.read(image_buffer, 16);
  99.         in_file.read(file_in_buffer, 1);
  100.  
  101.         if(in_file)
  102.         {
  103.             operand = 1;
  104.  
  105.             for(j = 0; j <8; j++)
  106.             {
  107.                 if((file_in_buffer[0] & operand) != 0)    //Copying the file_in_buffer to the array.
  108.                     bits[j] = 1;
  109.                 else
  110.                     bits[j] = 0;
  111.                 operand = operand << 1;
  112.             }
  113.  
  114.             k=0;
  115.  
  116.             for(j = 0; j < 16; j++)
  117.             {
  118.                 if(j%4 == 0 || j%4 == 1)
  119.                 {
  120.                     image_buffer[j] = image_buffer[j] & (-2);        //Making the last bit 0.
  121.                     image_buffer[j] = image_buffer[j] ^ bits[k];
  122.                     k++;
  123.                 }
  124.             }
  125.         }
  126.  
  127.         out.write(image_buffer, 16);
  128.         i++;
  129.  
  130.     }while(in_image);
  131.  
  132.     in_image.close();
  133.     in_file.close();
  134.     out.close();
  135.  
  136.     delete image_header;
  137.     delete image_buffer;
  138.     delete file_in_buffer;
  139.     delete bits;
  140. }    
  141.  
  142. void Decrypt()
  143. {
  144.     system("cls");
  145.  
  146.     ifstream image;
  147.     ofstream out;
  148.  
  149.     char* im_in = new char[200];
  150.     char* f_out = new char[200];
  151.  
  152.     cout << "Enter input image file(.tga) : " ;
  153.     cin >> im_in;
  154.     cout << "Enter output file : " ;
  155.     cin >> f_out;
  156.  
  157.     image.open(im_in, ios::in | ios::binary);
  158.     out.open(f_out, ios::out|ios::binary);
  159.  
  160.     delete im_in;
  161.     delete f_out;
  162.  
  163.     char image_header[54];
  164.     char image_buffer[16];
  165.     char* temp = new char[1];
  166.     int tempi;
  167.  
  168.     int bits[8];
  169.     int i, j, k;
  170.  
  171.     int operand;
  172.  
  173.     image.read(image_header, 54);
  174.     int f_length = 0;
  175.  
  176.     for(i = 0; i <1; i ++)
  177.     {
  178.         if(image_header[i+5] > -1)
  179.             f_length += image_header[i+5]*pow(256, i); 
  180.         else
  181.             f_length += (256 + image_header[i+5])*pow(256, i);
  182.     }
  183.  
  184.  
  185.     i = 0;
  186.     for(i = 0; i < f_length; i++)
  187.     {
  188.  
  189.         for(j = 0; j < 8; j++)
  190.         {
  191.             bits[j] = 0;
  192.         }
  193.  
  194.         image.read(image_buffer, 16);
  195.  
  196.  
  197.         k = 0;
  198.  
  199.         for(j = 0; j <16; j++)
  200.         {
  201.             if(j%4 == 0 || j%4 == 1)
  202.             {
  203.                 bits[k] = image_buffer[j] & 1;
  204.                 k++;
  205.             }
  206.  
  207.         }
  208.  
  209.         tempi = 0;
  210.         operand = 1;
  211.  
  212.         for(j = 0; j < 8; j++)
  213.         {
  214.             tempi += (bits[j]<<j) & operand;
  215.             operand = operand << 1;
  216.         }
  217.  
  218.         temp[0] = (char)tempi;
  219.  
  220.         out.write(temp, 1);
  221.  
  222.  
  223.     }while(image);
  224.  
  225.     image.close();
  226.     out.close();
  227.  
  228.     delete image_header;
  229.     delete image_buffer;
  230. //    delete temp;
  231. }
  232.  
Mar 9 '07 #3

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

Similar topics

5
by: Rob Somers | last post by:
Hey all I am writing a program to keep track of expenses and so on - it is not a school project, I am learning C as a hobby - At any rate, I am new to structs and reading and writing to files,...
32
by: someone else | last post by:
hi all I'm a newbie to this group. my apologies if I break any rules. I've wrote a simple program to find the first 1,000,000 primes, and to find all primes within any range (up to 200 *...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
20
by: Chris LaJoie | last post by:
I'm looking for some kind of simple string compression code I can use. I'm not looking for SharpZipLib. Their implimentation spans several classes and is very complex. I'm just looking for...
1
by: John Dann | last post by:
I need to read a binary file (whose structure I know - at least in generic terms) and just learning about how best to do it. Looking at deserialisation this seems to be one direct approach. But...
28
by: Tim Daneliuk | last post by:
I have a program wherein I want one behavior when a file is set as executable and a different behavior if it is not. Is there a simple way to determine whether a given named file is executable...
1
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template...
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.