From binary to text and from text to binary

Started by
3 comments, last by Floru 22 years, 5 months ago
I have a simple question. If I read a binary file in text mode can I write it back in binary form so that the content is still the same? Floru
Advertisement
quote:Original post by Floru

I have a simple question. If I read a binary file in text mode can I write it back in binary form so that the content is still the same?

Floru


I''m not sure I understood what you meant but from what I can make out, if you read a binary file, it''s just data, stored in a struct. So you could rewrite that struct in another file, or if you''re sure that it won''t change, you can re-write it in the same file at the same location.



"And that''s the bottom line cause I said so!"

Cyberdrek
Headhunter Soft
A division of DLC Multimedia

Resist Windows XP''s Invasive Production Activation Technology!

"gitty up" -- Kramer
[Cyberdrek | ]

More specific:
First I''d like to read a binary file in text form/mode. Like if you open a binary file with notepad.

My question is:
If I have this text data can I save it back in binary form so that the original content has not changed?

Example:
1. Open test.bmp in text mode and save to test.txt file
2. Open test.txt file and save it as test2.bmp in binary form
3. test2.bmp is the same as test.bmp

Floru
Well, notepad will certiainly garble it.

Most C programs have trouble with binary data in text mode, since if you read it as a string the string functions expect a null character to end the string, but a large sample of random binary data will probably contain several null characters.


Let''s turn the question around. Why do you want to do this?
Simple answer: no.

Reason: when you open a file in text mode, it converts \r\n combinations to \n. If you have a \n alone, it''s unchanged. There''s no way to tell which of your \n were \r\n or just \n, so you''re hosed.

This topic is closed to new replies.

Advertisement