2012-12-16 1 views
1

파일에서 읽은 행을 문자열 목록으로 자르려고합니다. 이것은 항상 해결할 수없는 예외를 발생시킵니다.문자열 뒤의 erlang 예외 : 파일에서 읽은 행의 토큰

exception error: no function clause matching string:tokens1 
(<<"Cascading Style Sheets CSS are an increasingly common way for website developers to control the look\n">>," ,.",[]) in function readTest:run/1 



-module(readTest). 
-export([run/1]). 

open_file(FileName, Mode) -> 
    {ok, Device} = file:open(FileName, [Mode, binary]), 
    Device. 

close_file(Device) -> 
    ok = file:close(Device). 

read_lines(Device, L) -> 
    case io:get_line(Device, L) of 
     eof -> 
      lists:reverse(L); 
     String -> 
      read_lines(Device, [String | L]) 
    end. 

run(InputFileName) -> 
    Device = open_file(InputFileName, read), 
    Data = read_lines(Device, []), 
    close_file(Device), 
    io:format("Read ~p lines~n", [length(Data)]), 
    Bla = string:tokens(hd(Data)," ,."), 
    io:format(hd(Data)). 

쉽게 실패 할 수 있습니다. 에를랭에서 막 시작했습니다.

답변

1

바이너리 플래그가있는 파일을 열면 행이 목록 (문자열)이 아닌 바이너리로 읽혀집니다. 토큰 충돌 : 그래서 코드에서

Bla = string:tokens(hd(Data)," ,."), 

HD (데이터) 실제로 문자열이 발생 바이너리입니다. 개방, 또는 명시 적으로 나열하는 바이너리로 변환 : 당신은 파일의 바이너리 플래그를 제거 할 수 있습니다 중 하나

Bla = binary:split(Data, [<<" ">>, <<",">>, <<".">>], [global]) 

(the documentation for binary:split/3를 참조하십시오

Bla = string:tokens(binary_to_list(hd(Data))," ,."), 
+0

(도움을 청합니다.) 어리석은 실패 – prototyp

1

그것은 목록에 변환하지 않고 바이너리를 분리 할 수도 있습니다 .)

관련 문제