Erlang从文件中读取前5行

时间:2011-02-05 05:02:36

标签: file file-io erlang

例如,我有包含10个文本字符串的txt文件。如何用erlang读取本文的前5个字符串?

谢谢。

3 个答案:

答案 0 :(得分:8)

您可能希望file:open/2file:read_line/1的组合启用缓冲。

押韵:

$ cat mary_lamb.txt
Mary had a little lamb,
little lamb, little lamb,
Mary had a little lamb,
whose fleece was white as snow.
And everywhere that Mary went,
Mary went, Mary went,
and everywhere that Mary went,
the lamb was sure to go.

源文件:

$ cat ./read_n_lines.erl
-module(read_n_lines).
-export([read_n_lines/2]).

read_n_lines(Filename,NumLines) ->
     {ok, FileDev} = file:open(Filename, 
          [raw, read, read_ahead]),
     Lines = do_read([],FileDev, NumLines),
     file:close(FileDev),
     Lines.

do_read(Lines, _, 0) ->
     lists:reverse(Lines);
do_read(Lines, FileDev, L) ->
     case file:read_line(FileDev) of
          {ok, Line} ->
               do_read([Line|Lines], FileDev, L - 1);
          eof ->
               do_read(Lines, FileDev, 0)
     end.
raw中的

Modes传递给file:open/2,允许更快地访问文件,因为不需要Erlang进程来处理文件。

示例运行:

$ erl
1> c(read_n_lines).
{ok,read_n_lines}
2> Lines = read_n_lines:read_n_lines("./mary_lamb.txt", 5).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n"]
3> length(Lines).
5
4> read_n_lines:read_n_lines("./mary_lamb.txt", 666).
["Mary had a little lamb,\n","little lamb, little lamb,\n",
 "Mary had a little lamb,\n",
 "whose fleece was white as snow.\n",
 "And everywhere that Mary went,\n",
 "Mary went, Mary went,\n",
 "and everywhere that Mary went,\n",
 "the lamb was sure to go."]
5> 

要从字符串中删除换行符,您可以使用string:strip/1,2,3

5> lists:map(fun(X) -> string:strip(X, right, $\n) end, Lines).
["Mary had a little lamb,","little lamb, little lamb,",
 "Mary had a little lamb,",
 "whose fleece was white as snow.",
 "And everywhere that Mary went,"]
6>

答案 1 :(得分:2)

另一种解决方案,n_times可以在其他地方使用:

-module(n_times).

-export([test/0]).

test() ->
  io:format("~p~n", [n_lines("n_times.erl", 5)]).

n_lines(FileName, N) ->
  {ok, FileDev} = file:open(FileName, [raw, read, read_ahead]),
  try
    n_times(fun() -> {ok, L} = file:read_line(FileDev), L end, N)
  after
    file:close(FileDev)
  end.

n_times(F, N) ->
  n_times(F, N, []).

n_times(_, 0, A) ->
  lists:reverse(A);
n_times(F, N, A) ->
  n_times(F, N-1, [F()|A]).

答案 2 :(得分:1)

使用erlang的io模块。

IO:读(FD, '')

其中FD是文件句柄。

另外,请查看erlang文档以获取正确的语法。

这是一个粗略的代码

func(FD) ->
case io:get_line(FD,'') of
{ok,text}->
 %%do something,
func(FD);
eof ->
%%exit;
error->
%%quit
end

如果只想处理10行,可以使用计数器