Click here to Skip to main content
13,594,948 members
Rate this:
Please Sign up or sign in to vote.
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
is mostly used.i want to change

How can i do that in c++?
Posted 4-Feb-13 22:04pm
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
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[^].
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:47am
That's pretty much it... +5
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.

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 | Cookies | Terms of Service
Web04 | 2.8.180621.3 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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