Borrar filtros
Borrar filtros

Overwriting a binary file

2 visualizaciones (últimos 30 días)
Ronaldo
Ronaldo el 30 de Sept. de 2013
Comentada: Image Analyst el 1 de Oct. de 2013
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')

Respuestas (2)

Image Analyst
Image Analyst el 1 de Oct. de 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 comentarios
Ronaldo
Ronaldo el 1 de Oct. de 2013
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 el 1 de Oct. de 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().

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 1 de Oct. de 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 comentarios
Ronaldo
Ronaldo el 1 de Oct. de 2013
Editada: Walter Roberson el 1 de Oct. de 2013
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 el 1 de Oct. de 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};

Iniciar sesión para comentar.

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by