1
-module(test).
-export([run/1]).
open_file(FileName, Mode) ->
{ok, Device} = file:open(FileName, [Mode, binary]), Device.
close_file(Device) ->
ok = file:close(Device).
read(File) ->
case file:read_line(File) of
{ok, Data} -> [Data | read(File)];
eof -> []
end.
run(InputFileName) ->
Device = open_file(InputFileName, read),
Data = read(Device),
[First |TheRest] = Data,
io:format("First line is ~p ~n", [First]),
close_file(Device).
d1 and is program program the
d2 a apply copyleft free free freedom
d3 copyleft copyleft share share users
d4 released software works
d5 licenses licenses licenses licenses licenses software
d8 apply
莫名其妙地變成
50> test:run("input.txt").
First line is <<"d1\tand\tis\tprogram\tprogram\tthe\n">>
ok
這是代表一個列表的一種特殊的方式?還是我需要使用某種模塊將讀取轉換爲列表?
我的最終目標是使密鑰對的列表:
{d1 [and is program program the]}
謝謝!
{ok, Device} = file:open(FileName, [Mode, binary]), Device.
如果將其更改爲:
嘿!我碰巧看過我的「Erlang編程」書的後面。 – 7stud