How do I fix this code to copy text from a number of text files to a bunch of other text files?
19 次查看(过去 30 天)
显示 更早的评论
I have a number of text files in a directory. I want to copy the contents of each of these text files into brand new text files except I do not want to copy the first 4 lines. For this I have written this code but it is not working. Can you help me fix this?
files = dir('*.TXT')
N = numel(files)
count = 0;
for i = 1:N
fid1 = files(i).name
disp(fid1)
a = fopen(fid1,'r')
file_name = [sprintf('%1.0f',i) '.txt'];
fopen(file_name,'w+');
for K = 1:4
inline = fgetl(a)
end
while ischar(inline)
inline = fgetl(a)
fprintf(file_name,'%s\n\r',inline)
end
fclose('all')
end
0 个评论
采纳的回答
dpb
2017-8-4
for i = 1:length(files)
disp(fid1)
fidI = fopen(files(i).name,'r');
file_name = [sprintf('%1.0f',i) '.txt']; % would recommend use a more descriptive name
fidO=fopen(file_name,'w+'); % you didn't save the file handle...
for K = 1:4, inline=fgetl(a); end % skip the four records
while ischar(inline)
inline = fgets(a); % get line, include \n
fprintf(fidO,'%s',inline) % write output; \n is in string by dint of fgets
end
fclose('all')
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!