fidrrt = fopen('F:\lyn.txt');
while 1
tline = fgetl(fidrrt);
if ~ischar(tline), break, end
tline = native2unicode(tline);
disp(tline)
end
fclose(fidrrt);
经测试正确
输出:
你好 你好 的 ; 你们 %第一行
, 好 %第二行
循环读取一个文件夹中制定后缀的文件方法:
clc;
clear all;
载入文件夹
pathname = uigetdir(cd, '请选择文件夹');
if pathname == 0
msgbox('您没有正确选择文件夹');
return;
end
%搜索指定格式文件
pathname = strcat(pathname, '\');
[read_file_list] = batch_read_file(pathname, '*.txt');
len = length(read_file_list);
%% 循环读取每个文件
for i = 1:len
filepath = read_file_list(i);
fidrrt = fopen('f:\1business.seg.cln.txt');
while 1
tline = fgetl(fidrrt);
if ~ischar(tline), break, end
tline = native2unicode(tline);%% 中文显示正常,文件可以正常读完。
disp(tline)
end
fclose(fidrrt);
end
read_file_list 函数如下:
function [read_file_list] = batch_read_file(direction, file_type)
%
file_read = dir(fullfile(direction, file_type)); %获取要读取的文件列表
if strcmp(file_type, '*.txt'); %读取txt文件
for i = 1:length(file_read)
read_file_list{i} = strcat(direction, file_read(i).name); %获取文件名的列表
end
end
end
转载请注明出处:
http://blog.csdn.net/ningyaliuhebei/article/details/46342087