%%============================================%%
%% 对文件进行遍历,以行为单位进行输出,没一行构成一个列表元素,
%% 最终拼成一个列表。 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), []}.
erlang for_each_file
最新推荐文章于 2020-12-30 13:44:29 发布