erlang-读取文件-2

 

 

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]).
  
    

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值