erlang-读取文件-2
程序员文章站
2022-04-29 23:42:39
...
1.compile and run
Eshell V5.8.2 (abort with ^G)
1> c(sfile).
./sfile.erl:14: Warning: variable 'Why' is unused
{ok,sfile}
2> c(readfile).
{ok,readfile}
3> readfile:start("../test/test1.po")
3> .
load.....
load ../test/test1.po finished.
list xx
run finished
ok
4>
2.sfile.erl
-module(sfile). -export([load/1,readline/1,unload/1]). -import(io,[get_line/2,atom/0]). -import(file,[open/2,close/1]). -vsn(0.001). -author({deepfuture}). %文件操作 %load加载,readline读取一行,unload关闭文件 load(Sfilename)-> case open(Sfilename,read) of {ok,Sf}->Sf; {error,Why}->throw(err) end. readline(Sf)->get_line(Sf,""). unload(Sf)->close(Sf).
3.
-module(readfile). -export([start/1]). -import(io,[fwrite/1,format/2]). -import(sfile,[load/1,unload/1,readline/1]). -import(string,[len/1,substr/3,to_lower/1,strip/1]). -vsn(0.001). -author({deepfuture}). %解析器 start(Sfilename)->%开始解析,参数是源文件名 fwrite("load.....~n"), try load(Sfilename) of Sf->format("load ~s finished.~n",[Sfilename]), read(Sf), unload(Sf) catch err->format("load ~s error~n",[Sfilename]) end. read(Sf)->%分别读取每行 case readline(Sf) of eof->format("run finished~n",[]); Data->myprint(Data), read(Sf) end. myprint(Data)->format("~s",[Data]).