Click here to Skip to main content
12,501,833 members (52,328 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ binary VisualC++
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 4-Feb-13 22:04pm
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^].
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100