Overwriting a binary file

조회 수: 7 (최근 30일)
Ronaldo
Ronaldo 2013년 9월 30일
댓글: Image Analyst 2013년 10월 1일
I opened a binary file by using a hxd editor software. I copied the Hex values to a text file. I want to overwrite the values in the original binary file but I cannot. Please find an image that I took from the input in text file, the original binary file and the overwritten binary file from the following link:
https://www.dropbox.com/sh/4m36p669u1zyn8y/O9Tn1Gu9od
Here is the code that I wrote:
I=textread('Text.txt', '%s','delimiter', '\n');
for i=1:size(I,1)
B=cell2mat(I(1,1));
D(i,:)=B;
end
fid = fopen('OriginalCopy.blo','w+');
fwrite(fid,D);
fclose(fid);
visdiff( 'Original.blo','OriginalCopy.blo')

답변 (2개)

Image Analyst
Image Analyst 2013년 10월 1일
Maybe I don't understand what you want to do but it sounds like you took a binary file and looked at it in hexadecimal mode (in some kind of editor program) and wrote the hex numbers out to a file as ASCII characters - a plain text file. Is that correct? Now you want that to replace your original binary input file, right? So why don't you just delete the original file and rename this text file with the name of the original binary file?
  댓글 수: 2
Ronaldo
Ronaldo 2013년 10월 1일
The answer of your first two questions is "YES".
The binary file is an input for a software. That is the reason I need to learn how to write a binary file (I want to change some values in the original file and then use the binary file again). I tried to overwrite the original file with the hope that it would be easier but it seems not.
Image Analyst
Image Analyst 2013년 10월 1일
Well then you definitely don't want to view it in hex and then write out a text version of the hex files!!! Just read the whole thing into an array, change the array, and then write out with fwrite().

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2013년 10월 1일
fid = fopen('Original.blo', 'w');
The binary file is now overwritten with an empty file. You can now fwrite() whatever you want to it.
Is the question how to replace just a part of the file, in the middle, with something of exactly the same size? If so, then see fseek()
  댓글 수: 2
Ronaldo
Ronaldo 2013년 10월 1일
편집: Walter Roberson 2013년 10월 1일
I do not know what's wrong that when I do the same action I get a very different result. Please look at the image that uploaded to the following link to see the problem that I have.
Walter Roberson
Walter Roberson 2013년 10월 1일
You have
B=cell2mat(I(1,1));
which is always converting the same string.
Recall too that cell2mat() is not going to convert the string to a binary value. Indeed, in that context, equivalent code to what you wrote would be
B = I{1,1};

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by