Overwriting a binary file
조회 수: 6 (최근 30일)
이전 댓글 표시
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')
댓글 수: 0
답변 (2개)
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
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
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
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 Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!