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

Simple binary file IO

P: 1
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
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
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

P: 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

Post your reply

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