2011-02-05 12 views
6

たとえば、テキストの10文字列のtxtファイルがあります。私はこのテキストの最初の5文字列をerlangでどのように読むことができますか?Erlangファイルから最初の5行を読み取る

ありがとうございます。

+0

@OPあなたが回答を受け入れるように要求されています。投稿された回答のいずれかがあなたの質問に答えて、あなたが満足していればそれを。 – Arunmu

+0

@ArunMu:ニックネームの前に '@ 'がなくても、彼(彼女)の投稿にコメントが追加されます。 ;-) –

答えて

8

を処理したい場合はここで

が荒いコードが

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

であるあなたは、おそらくあなたは、バッファリングと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. 

ソースファイル:Modesで、file:open/2に渡され、ファイルへの高速アクセスを可能にする

raw

$ 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. 
、何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

erlangのioモジュールを使用してください。

io:read(FD、 '')。

ここで、FDはファイルハンドルです。

正しい構文については、erlangのドキュメントも参照してください。あなたはわずか10行

+0

私はあなたが文字列を言及しているか、あなたが言うことを意図した行ですか?文字列であれば、最初の行を読み込み、erlangの文字列モジュールを使ってトークン化してください。またはビット操作を使用してください: – Arunmu

+0

これは文字列ではなく、用語を読みます:-) "用語を読みます。用語'を標準入力(' IoDevice')から取り出し、 'Prompt'でプロンプトを出します。 –

+0

@ Yasir:ohhk..thanksは私に気づかせるために..私は自分のコードを編集しました.. – Arunmu

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]). 
関連する問題