Hi there,
Can anyone give me a very simple example of how to write a string of characters into a binary file?
Thanks.
Can anyone give me a very simple example of how to write a string of characters into a binary file?
Thanks.
ofstream out; out.open(im_out, ios::out|ios::binary);
#include <iostream.h> #include <fstream> #include <string> #include <stdlib.h> #include <sys/stat.h> #include <math.h> #include <windows.h> using namespace std; void Encrypt(); void Decrypt(); int main(int argc, char* argv[]) { while(true){ system("cls"); cout << "1. Encrypt" << endl; cout << "2. Decrypt" << endl; cout << "3. Exit" << endl; int choice; cout << endl << "Enter choice : " ; cin >> choice; if(choice == 1) Encrypt(); else if(choice == 2) Decrypt(); else exit(0); } return 0; } void Encrypt() { ifstream in_image; ifstream in_file; ofstream out; system("cls"); char* im_in = new char[200]; char* f_in = new char[200]; char* im_out = new char[200]; cout << "Enter input image file(.tga) : " ; cin >> im_in; cout << "Enter input file : " ; cin >> f_in; cout << "Enter output file : " ; cin >> im_out; in_image.open(im_in, ios::in|ios::binary); in_file.open(f_in, ios::in|ios::binary); out.open(im_out, ios::out|ios::binary); // struct stat results; // if(stat(f_in, &results) != 0) // { // cout << "Error opening input file!" << endl; // exit(1); // } delete im_in; delete im_out; delete f_in; int i, j, k; char* image_header = new char[54]; char* image_buffer = new char[16]; char* file_in_buffer = new char[1]; char temp; int *bits = new int[8]; //Array to keep the bits in a byte. int operand; in_image.read(image_header, 54); int f_length = 0; image_header[5] = (char)f_length%256; image_header[6] = (char)(f_length/256)%256; image_header[7] = (char)(f_length/256)/256; out.write(image_header, 54); i = 0; do { in_image.read(image_buffer, 16); in_file.read(file_in_buffer, 1); if(in_file) { operand = 1; for(j = 0; j <8; j++) { if((file_in_buffer[0] & operand) != 0) //Copying the file_in_buffer to the array. bits[j] = 1; else bits[j] = 0; operand = operand << 1; } k=0; for(j = 0; j < 16; j++) { if(j%4 == 0 || j%4 == 1) { image_buffer[j] = image_buffer[j] & (-2); //Making the last bit 0. image_buffer[j] = image_buffer[j] ^ bits[k]; k++; } } } out.write(image_buffer, 16); i++; }while(in_image); in_image.close(); in_file.close(); out.close(); delete image_header; delete image_buffer; delete file_in_buffer; delete bits; } void Decrypt() { system("cls"); ifstream image; ofstream out; char* im_in = new char[200]; char* f_out = new char[200]; cout << "Enter input image file(.tga) : " ; cin >> im_in; cout << "Enter output file : " ; cin >> f_out; image.open(im_in, ios::in | ios::binary); out.open(f_out, ios::out|ios::binary); delete im_in; delete f_out; char image_header[54]; char image_buffer[16]; char* temp = new char[1]; int tempi; int bits[8]; int i, j, k; int operand; image.read(image_header, 54); int f_length = 0; for(i = 0; i <1; i ++) { if(image_header[i+5] > -1) f_length += image_header[i+5]*pow(256, i); else f_length += (256 + image_header[i+5])*pow(256, i); } i = 0; for(i = 0; i < f_length; i++) { for(j = 0; j < 8; j++) { bits[j] = 0; } image.read(image_buffer, 16); k = 0; for(j = 0; j <16; j++) { if(j%4 == 0 || j%4 == 1) { bits[k] = image_buffer[j] & 1; k++; } } tempi = 0; operand = 1; for(j = 0; j < 8; j++) { tempi += (bits[j]<<j) & operand; operand = operand << 1; } temp[0] = (char)tempi; out.write(temp, 1); }while(image); image.close(); out.close(); delete image_header; delete image_buffer; // delete temp; }
Comment