[英]How to read single line from file in Erlang
我是Erlang和stackoverflow的新手。 我一直在搜索有關如何使用Erlang從.txt文件中讀取字符串的線程。 我還想使用string:tokens
將其分成單詞,有人告訴我可以使用io:get_line來完成此操作,但是我必須做錯了什么。 這是我寫的代碼。 任何指導將不勝感激!
-module(lab6).
-export([file/1]).
file(fName) ->
file:open(fName, [read]),
string:tokens(io:get_line(fName), ". ").
-module(lab6).
-export([file/1]).
file(FName) -> % a variable must start by an Upper case character, otherwise it is an atom
{ok,IoDevice} = file:open(FName, [read]), % file:open/2 returns the tuple {ok,IoDevice} if it succeeds.
% IoDevice is the file descriptor you will use for further accesses
string:tokens(io:get_line(IoDevice,""), ". "). % you must use the file descriptor to read a new line, get_line
% is expecting 2 arguments, the second one is a prompt, not used here
% this code will split the first line of the file FName using
% the dot and the white space as separators. It will then returns
% the results letting the file open, but with the file descriptor
% lost! so no chance to continue to read the lines like this.
您可以看看向您學習一些erlang ,這是學習Erlang的絕佳站點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.