how to add additional content to a binary file

조회 수: 5 (최근 30일)
zhang
zhang 2011년 9월 9일
Hi guys,
I'd like to add some additional information to a binary file. The only requirement is to put these additional information at the beginning of the binary file.
Can anyone give some hints?
Thanks,
  댓글 수: 1
Jan
Jan 2011년 9월 9일
The question is not clear. Are you able to create the file in the wanted format, such that just the appending of the old contents is the problem?

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

답변 (1개)

Jan
Jan 2011년 9월 9일
There is no method to shift the contents of a file. Therefore the only method is creating a new file, write the new data and append the contents of the old file. Example:
% Create original file:
fid = fopen('File', 'w');
fwrite(fid, rand(1, 10));
fclose(fid);
% Create new file:
fid2 = fopen('File2', 'w');
fwrite(fid2, rand(1, 10));
% Read old file and append it:
fid = fopen('File', 'r');
data = fread(fid, inf, 'uint8');
close(fid);
fwrite(fid2, data, 'uint8');
fclose(fid2);
Now you can delete 'File' and rename 'File2' to 'File' on demand. An equivalent approach would be to read the old file at first and overwrite it directly. But in case of errors the former contents is lost.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by