Overwriting a binary file
    5 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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 commentaires
Réponses (2)
  Image Analyst
      
      
 le 1 Oct 2013
        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 commentaires
  Image Analyst
      
      
 le 1 Oct 2013
				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
      
      
 le 1 Oct 2013
        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 commentaires
  Walter Roberson
      
      
 le 1 Oct 2013
				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};
Voir également
Catégories
				En savoir plus sur Data Import and Export dans Help Center et File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


