tags:

views:

379

answers:

4

I am trying to read a bmp file in C++(Turbo). But i m not able to print binary stream.

I want to encode txt file into it and decrypt it. How can i do this. I read that bmp file header is of 54 byte. But how and where should i append txt file in bmp file. ?

I know only Turbo C++, so it would be helpfull for me if u provide solution or suggestion related to topic for the same.

int main()
{
ifstream fr; //reads
ofstream fw; // wrrites to file
char c;
int random;
clrscr();
char file[2][100]={"s.bmp","s.txt"};

fr.open(file[0],ios::binary);//file name, mode of open, here input mode i.e. read only
if(!fr)
    cout<<"File can not be opened.";
fw.open(file[1],ios::app);//file will be appended
if(!fw)
    cout<<"File can not be opened";
while(!fr)
    cout<<fr.get(); // error should be here. but not able to find out what error is it

fr.close();
fw.close();
getch();
}

This code is running fine when i pass txt file in binary mode

EDIT :

while(!fr)
    cout<<fr.get(); 

I am not able to see binary data in console this was working fine for text when i was passing character parameter in fr.get(c)

+1  A: 

With steganography, what little I know about it, you're not "appending" text. You're making subtle changes to the pixels (shading, etc..) to hide something that's not visually obvious, but should be able to be reverse-decrypted by examining the pixels. Should not have anything to do with the header. So anyway, the point of my otherwise non-helpful answer is to encourage you go to and learn about the topic which you seek answers, so that you can design your solution, and THEN come and ask for specifics about implementation.

Chris Thornton
@Chris: my major concern right now is to print binary stream on console. On which i can perform further operations. If you could help. It will be great
Shantanu Gupta
Ok, then your actual question should be along the lines of:How to read .bmp file and display the bytes as text, preferably as Hex characters?
Chris Thornton
@Chris: I want to print the output in binary for (0,1)
Shantanu Gupta
+1  A: 

You need to modify the bit pattern, not append any text to the file. One simple example : Read the Bitmap Content (after header), and sacrifice a bit from each of the byte to hold your content

Sr7
A: 

If on Windows, recode to use CreateFile and see what the real error is. If on Linux, ditto for open(2). Once you have debugged the problem you can probably shift back to iostreams.

bmargulies
+1  A: 

I think you question is allready answered: http://stackoverflow.com/questions/1024389/print-an-int-in-binary-representation-using-c

convert your char to an int and you are done (at least for the output part)

penguinpower