1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| %%============================================%% %% 对文件进行遍历,以行为单位进行输出,没一行构成一个列表元素, %% 最终拼成一个列表。 edit code by lgh %%============================================%%
-module(for_each_file). % -compile([export_all]). -export([start/1]).
start(FileName) -> {ok, Bin} = file:read_file(FileName), L = binary_to_list(Bin), io:format("The File Prototype is -> ~p~n",[L]), %% 文件原型列表 find_word(L, []).
find_word([], Acc) -> io:format("The result is -> ~p~n",[lists:reverse(Acc)]), %% 最终效果列表 ok;
find_word(L, Acc) -> {Word, L0} = for_each_word(L, []), Acc0 = [Word | Acc], find_word(L0, Acc0).
%% 遇到换行符,返回单词和剩余的列表。 for_each_word([$\n | T], L) -> {lists:reverse(L), T};
for_each_word([H|T], L) -> for_each_word(T, [H|L]);
for_each_word([], L) -> {lists:reverse(L), []}.
|