Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
How to read binary file and change it.
For example:
Every data/file in our computer have its binary source which is only 0s and 1s.
I have a binary file in which
01000100
is mostly used.i want to change
01000100
with
01100100

How can i do that in c++?
Posted
Comments
Matthew Faithfull 5-Feb-13 4:18am    
There will clearly be 3 stages to any solution.
Reading the file.
Examining and changing the data that's been read.
Writing the changed data back to the file.
I suggest you research the first stage, reading, try to adapt one of the many samples usages of istream or CFile or fread to your situation and if you can't make it work ask a specific question with some code attached to give people a good chance to help you.
anonymous10 5-Feb-13 4:35am    
Can you please give me little bit code example.

1 solution

If you need to change the content of a file (that is overwrite it) then you have to:
  1. Read the file content into memory.
  2. Modify memory content.
  3. Write back modified memory content to the original file.


You may use the C++ fstream to read/write a binary file, see, for instance Input/Output with files[^] for code samples.

You may as well use the C-like functions fopen,fwrite,fread for doing the same. See, for instance fopen[^] fwrite[^], fread[^].
 
Share this answer
 
Comments
anonymous10 5-Feb-13 4:25am    
Can you please give me little bit code example.
CPallini 5-Feb-13 4:57am    
You may find code samples in the documentation pages I linked in my answer.
Albert Holguin 5-Feb-13 11:49am    
Do a bit of searching online after people give you solutions... you can't expect us to spoon feed you. We post answers mostly off the top of our heads and we do it all for free because we like to help, meaning we don't like to spend a lot of time on each individual question.
Albert Holguin 5-Feb-13 11:47am    
That's pretty much it... +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900