Overwriting a binary file
4 次查看(过去 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 中查找有关 String Parsing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!