将两个文件位置之间的二进制文件的一部分复制到Matlab中的新文件中
我有一个二进制文件,开头有一些垃圾。我正在使用MATLAB找到好东西的开始,并希望将文件的其余部分从位置1388复制到一个新文件的结尾。我知道我可以很容易地把十六进制的文件读入一个新的文件,但是我循环了很多大文件,我会经常这样做,所以我希望有人知道一些方法,把部分文件转储到新文件中将两个文件位置之间的二进制文件的一部分复制到Matlab中的新文件中,matlab,file-io,binary,copy,Matlab,File Io,Binary,Copy,我有一个二进制文件,开头有一些垃圾。我正在使用MATLAB找到好东西的开始,并希望将文件的其余部分从位置1388复制到一个新文件的结尾。我知道我可以很容易地把十六进制的文件读入一个新的文件,但是我循环了很多大文件,我会经常这样做,所以我希望有人知道一些方法,把部分文件转储到新文件中 fidin = fopen('input.txt','r'); fseek(fidin, 1388, 'bof'); bulksize = 8192; count = bulksize; fid = fopen('o
fidin = fopen('input.txt','r');
fseek(fidin, 1388, 'bof');
bulksize = 8192;
count = bulksize;
fid = fopen('output.txt', 'a');
while (count >= bulksize) do
[A count] = fread(fidin, bulksize, '*uint8');
fwrite(fid, A);
end_while
fclose(fidin);
fclose(fid);
input.txt输入文件
Output.txt输出文件
1388距文件开头的偏移量
对于较大的文件,请微调体积大小
input.txt输入文件
Output.txt输出文件
1388距文件开头的偏移量
对于较大的文件,请微调体积大小
input.txt输入文件
Output.txt输出文件
1388距文件开头的偏移量
对于较大的文件,请微调体积大小
input.txt输入文件
Output.txt输出文件
1388距文件开头的偏移量
对于更大的文件,我认为amald的想法是正确的(+1),但我的经验告诉我,“当现实降临时”,他的实现无疑会让你受到伤害: 以下是一个(IMO)更好的方法:
function yourFcn
% File names
inputFile = 'input.bin';
outputFile = 'output.bin';
% The computed offset (ideally, this is also done inside a try/catch)
offset = 1388;
% Amount of bytes per read
N = 8192;
% Open the files
fin = fopen(inputFile, 'r');
assert(fin > 0, 'yourFcn:ioError',...
'Error opening input file.');
fout = fopen(outputFile, 'w+');
if fout < 0
ME = MException('yourFcn:ioError',...
'Error opening output file.');
throw( closeFile(fin, inputFile, ME) );
end
% Set file pointer to computed offset
try
fseek(fin, offset, 'bof');
catch ME
ME = addCause(ME, MException('yourFcn:seekFailure',...
'Error applying offset to input file'));
closeBoth(ME);
end
% Read N bytes at a time, and dump them in the new file
try
while ~feof(fin)
assert(fwrite(fout,fread(fin,N)) == N, 'yourFcn:ioError',...
'Mismatch between bytes read and bytes written.');
end
catch ME
ME = addCause(ME, MException('yourFcn:writeError',...
'Error writing to output file'));
closeBoth(ME);
end
closeBoth();
% Safely close one of the files
function ME = closeFile(fid, fileName, ME)
try
fclose(fid);
catch ME2
errMsg = sprintf(['Error closing file ''%s''; ',...
'all file handles have been forcibly closed.'], fileName);
if isempty(ME_in)
ME = MException('yourFcn:closeError', errMsg);
else
ME.message = char(ME.message, errMsg);
end
ME = addCause(ME2, ME);
fclose('all');
end
end
% Safely close both files
function ME = closeBoth(ME_in)
if nargin == 0
ME_in = []; end
ME = closeFile(fin, inputFile, ME_in);
if isequal(ME,ME_in)
ME = closeFile(fout, outputFile, ME); end
if ~isempty(ME)
throw(ME); end
end
end
函数yourFcn
%文件名
inputFile='input.bin';
outputFile='output.bin';
%计算的偏移量(理想情况下,这也是在try/catch中完成的)
偏移量=1388;
%每次读取的字节数
N=8192;
%打开文件
fin=fopen(输入文件“r”);
断言(fin>0,'yourFcn:ioError',。。。
“打开输入文件时出错。”);
fout=fopen(输出文件“w+”);
如果fout<0
ME=MEException('yourFcn:ioError',。。。
“打开输出文件时出错。”);
抛出(关闭文件(fin、inputFile、ME));
结束
%将文件指针设置为计算偏移量
尝试
fseek(fin,offset,'bof');
抓住我
ME=addCause(ME,MeException('yourFcn:seekFailure'),。。。
“将偏移量应用于输入文件时出错”);
两个(我);
结束
%一次读取N个字节,并将它们转储到新文件中
尝试
而~feof(fin)
断言(fwrite(fout,fread(fin,N))==N,‘yourFcn:ioError’,。。。
'读取的字节与写入的字节不匹配');
结束
抓住我
ME=addCause(ME,MeException('yourFcn:writeError',。。。
“写入输出文件时出错”);
两个(我);
结束
关闭两个();
%安全地关闭其中一个文件
函数ME=closeFile(fid、文件名、ME)
尝试
fclose(fid);
抓住我
errMsg=sprintf(['Error closing file'%s';',。。。
“所有文件句柄都已被强制关闭。”],文件名);
如果我是空的(我在)
ME=MException('yourFcn:closeError',errMsg);
其他的
ME.message=char(ME.message,errMsg);
结束
ME=addCause(ME2,ME);
fclose(“全部”);
结束
结束
%安全地关闭两个文件
功能ME=关闭两个(ME\u in)
如果nargin==0
ME_in=[];结束
ME=关闭文件(fin、inputFile、ME\u in);
如果相等(我,我在)
ME=关闭文件(fout、outputFile、ME);结束
如果我是空的(我)
扔(我);结束
结束
结束
我认为阿马尔德的想法是正确的(+1),但我的经验告诉我,“当现实降临时”,他的实施无疑会让你受到伤害:
以下是一个(IMO)更好的方法:
function yourFcn
% File names
inputFile = 'input.bin';
outputFile = 'output.bin';
% The computed offset (ideally, this is also done inside a try/catch)
offset = 1388;
% Amount of bytes per read
N = 8192;
% Open the files
fin = fopen(inputFile, 'r');
assert(fin > 0, 'yourFcn:ioError',...
'Error opening input file.');
fout = fopen(outputFile, 'w+');
if fout < 0
ME = MException('yourFcn:ioError',...
'Error opening output file.');
throw( closeFile(fin, inputFile, ME) );
end
% Set file pointer to computed offset
try
fseek(fin, offset, 'bof');
catch ME
ME = addCause(ME, MException('yourFcn:seekFailure',...
'Error applying offset to input file'));
closeBoth(ME);
end
% Read N bytes at a time, and dump them in the new file
try
while ~feof(fin)
assert(fwrite(fout,fread(fin,N)) == N, 'yourFcn:ioError',...
'Mismatch between bytes read and bytes written.');
end
catch ME
ME = addCause(ME, MException('yourFcn:writeError',...
'Error writing to output file'));
closeBoth(ME);
end
closeBoth();
% Safely close one of the files
function ME = closeFile(fid, fileName, ME)
try
fclose(fid);
catch ME2
errMsg = sprintf(['Error closing file ''%s''; ',...
'all file handles have been forcibly closed.'], fileName);
if isempty(ME_in)
ME = MException('yourFcn:closeError', errMsg);
else
ME.message = char(ME.message, errMsg);
end
ME = addCause(ME2, ME);
fclose('all');
end
end
% Safely close both files
function ME = closeBoth(ME_in)
if nargin == 0
ME_in = []; end
ME = closeFile(fin, inputFile, ME_in);
if isequal(ME,ME_in)
ME = closeFile(fout, outputFile, ME); end
if ~isempty(ME)
throw(ME); end
end
end
函数yourFcn
%文件名
inputFile='input.bin';
outputFile='output.bin';
%计算的偏移量(理想情况下,这也是在try/catch中完成的)
偏移量=1388;
%每次读取的字节数
N=8192;
%打开文件
fin=fopen(输入文件“r”);
断言(fin>0,'yourFcn:ioError',。。。
“打开输入文件时出错。”);
fout=fopen(输出文件“w+”);
如果fout<0
ME=MEException('yourFcn:ioError',。。。
“打开输出文件时出错。”);
抛出(关闭文件(fin、inputFile、ME));
结束
%将文件指针设置为计算偏移量
尝试
fseek(fin,offset,'bof');
抓住我
ME=addCause(ME,MeException('yourFcn:seekFailure'),。。。
“将偏移量应用于输入文件时出错”);
两个(我);
结束
%一次读取N个字节,并将它们转储到新文件中
尝试
而~feof(fin)
断言(fwrite(fout,fread(fin,N))==N,‘yourFcn:ioError’,。。。
'读取的字节与写入的字节不匹配');
结束
抓住我
ME=addCause(ME,MeException('yourFcn:writeError',。。。
“写入输出文件时出错”);
两个(我);
结束
关闭两个();
%安全地关闭其中一个文件
函数ME=closeFile(fid、文件名、ME)
尝试
fclose(fid);
抓住我
errMsg=sprintf(['Error closing file'%s';',。。。
“所有文件句柄都已被强制关闭。”],文件名);
如果我是空的(我在)
ME=MException('yourFcn:closeError',errMsg);
其他的
ME.message=char(ME.message,errMsg);
结束
ME=addCause(ME2,ME);
fclose(“全部”);
结束
结束
%安全地关闭两个文件
功能ME=关闭两个(ME\u in)
如果nargin==0
ME_in=[];结束
ME=关闭文件(fin、inputFile、ME\u in);
如果相等(我,我在)
ME=关闭文件(fout、outputFile、ME);结束
如果我是空的(我)
扔(我);结束
结束
结束
我认为阿马尔德的想法是正确的(+1),但我的经验告诉我,“当现实降临时”,他的实施无疑会让你受到伤害:
以下是一个(IMO)更好的方法:
function yourFcn
% File names
inputFile = 'input.bin';
outputFile = 'output.bin';
% The computed offset (ideally, this is also done inside a try/catch)
offset = 1388;
% Amount of bytes per read
N = 8192;
% Open the files
fin = fopen(inputFile, 'r');
assert(fin > 0, 'yourFcn:ioError',...
'Error opening input file.');
fout = fopen(outputFile, 'w+');
if fout < 0
ME = MException('yourFcn:ioError',...
'Error opening output file.');
throw( closeFile(fin, inputFile, ME) );
end
% Set file pointer to computed offset
try
fseek(fin, offset, 'bof');
catch ME
ME = addCause(ME, MException('yourFcn:seekFailure',...
'Error applying offset to input file'));
closeBoth(ME);
end
% Read N bytes at a time, and dump them in the new file
try
while ~feof(fin)
assert(fwrite(fout,fread(fin,N)) == N, 'yourFcn:ioError',...
'Mismatch between bytes read and bytes written.');
end
catch ME
ME = addCause(ME, MException('yourFcn:writeError',...
'Error writing to output file'));
closeBoth(ME);
end
closeBoth();
% Safely close one of the files
function ME = closeFile(fid, fileName, ME)
try
fclose(fid);
catch ME2
errMsg = sprintf(['Error closing file ''%s''; ',...
'all file handles have been forcibly closed.'], fileName);
if isempty(ME_in)
ME = MException('yourFcn:closeError', errMsg);
else
ME.message = char(ME.message, errMsg);
end
ME = addCause(ME2, ME);
fclose('all');
end
end
% Safely close both files
function ME = closeBoth(ME_in)
if nargin == 0
ME_in = []; end
ME = closeFile(fin, inputFile, ME_in);
if isequal(ME,ME_in)
ME = closeFile(fout, outputFile, ME); end
if ~isempty(ME)
throw(ME); end
end
end
函数yourFcn
%文件名
因普