Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ binary VC++
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 23:04pm
Comments
Matthew Faithfull at 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 at 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 at 5-Feb-13 4:25am
   
Can you please give me little bit code example.
CPallini at 5-Feb-13 4:57am
   
You may find code samples in the documentation pages I linked in my answer.
Albert Holguin at 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 at 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
0 DamithSL 375
1 Sergey Alexandrovich Kryukov 217
2 OriginalGriff 210
3 Peter Leow 135
4 Uma Shankar Patel 80
0 OriginalGriff 7,380
1 DamithSL 5,359
2 Sergey Alexandrovich Kryukov 4,944
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
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