programming erlang ch13 - andstudy/forge GitHub Wiki

์ œ 12์žฅ ํŒŒ์ผ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๊ตฌ์„ฑ

4๊ฐœ์˜ ๋ชจ๋“ˆ :

  • file
  • filename
  • filelib
  • io

ํŒŒ์ผ์„ ์ฝ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•

ํŒŒ์ผ ์•ˆ์˜ ๋ชจ๋“  ํ…€ ์ฝ๊ธฐ

    1> file:consult("data1.dat").
    {ok,[{person,"joe","armstrong"
                 [{occupation,programmer},{favoriteLanguage,erlang}]},
         {cat,{name,"zorro"},{owner,"joe"}}]}

ํŒŒ์ผ ์•ˆ์˜ ํ…€์„ ํ•œ ๋ฒˆ์— ํ•˜๋‚˜์”ฉ ์ฝ๊ธฐ

  • @spec file:open(File, read) => {ok, IoDevice} | {error, Why}

  • @spec file:read(IoDevice, Prompt) => {ok, Term} | {error, Why} | eof

  • @spec file:close(IoDevice) => ok | {error, Why}

      1> {ok, S} = file:open("data1.dat", read).
      {ok,<0.34.0>}
      2> io:read(S, '').
      {ok,{person,"joe","armstrong",
                  [{occupation,programmer},{favoriteLanguage,erlang}]}}
      3> io:read(S, '').
      {ok,{cat,{name,"zorro"},{owner,"joe"}} }
      4> io:read(S, '').
      eof
      5> file:close(S).
      ok
    

ํŒŒ์ผ ์•ˆ์˜ ๋ผ์ธ์„ ํ•œ ๋ฒˆ์— ํ•œ ์ค„์”ฉ ์ฝ๊ธฐ

    1> {ok, S} = file:open("data1.dat",  read).
    {ok,<0.43.0>}
    2> io:get_line(S, '').
    "{person, \"joe\", \"armstrong\",\n"
    3> io:get_line(S, '').
    "\t[{occupation, programmer},\n"
    4> io:get_line(S, '').
    "\t {favoriteLanguage, erlang}]}.\n"
    5> io:get_line(S, '').
    "\n"
    6> io:get_line(S, '').
    "{cat, {name, \"zorro\"},\n"
    7> io:get_line(S, '').
    "      {owner, \"joe\"}}.\n"
    8> io:get_line(S, '').
    eof
    9> file:close(s).
    ok

์ „์ฒด ํŒŒ์ผ์„ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ์ฝ๊ธฐ

    1> file:read_file("data1.dat").
    {ok,<<"{person, \"joe\", \"armstrong\",\n\t[{occupation, programmer},
    \n\t {favoriteLanguage, erlang}]}.\n\n{cat, {name, \"zorr"...>>}

๋žœ๋ค ์•ก์„ธ์Šค๋กœ ํŒŒ์ผ ์ฝ๊ธฐ

    1> {ok, S} = file:open("data1.dat", [read, binary, raw]).
    {ok,{file_descriptor,prim_file,{#Port<0.101>,1632}} }
    2> file:pread(S, 22, 46).
    {ok,<<"rong\",\n\t[{occupation, programmer},\n\t {favorite">>}
    3> file:pread(S, 1, 10).
    {ok,<<"person, \"j">>}
    4> file:pread(S, 2, 10).
    {ok,<<"erson, \"jo">>}
    5> file:close(S).
    ok

ํŒŒ์ผ์— ์“ฐ๋Š” ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•

ํ…€ ๋ฆฌ์ŠคํŠธ๋ฅผ ํŒŒ์ผ์— ์“ฐ๊ธฐ

  • @spec io:format(IoDevice, Format, Args) -> ok

      unconsult(File, L) ->
          {ok, S} = file:open(File, write),
          lists:foreach(fun(X) -> io:format(S, "~p.~n", [X]) end, L),
          file:close(S).
    

ํŒŒ์ผ์— ๋ผ์ธ ์“ฐ๊ธฐ

    1> {ok, S} = file:open("test2.dat", write).
    {ok,<0.32.0>}
    2> io:format(S, "~s~n", ["Hello readers"]).
    ok
    3> io:format(S, "~w~n", [123]).
    ok
    4> io:format(S, "~s~n", ["that's it"]).
    ok
    5> file:close(S).
    ok

ํ•œ ๋ฒˆ์˜ ์—ฐ์‚ฐ์œผ๋กœ ์ „์ฒด ํŒŒ์ผ ์“ฐ๊ธฐ

    -module(scavenge_urls).
    -export([urls2htmlFile/2, bin2urls/1]).
    -import(lists, [reverse/1, reverse/2, map/2]).
    
    
    
    urls2htmlFile(Urls, File) ->         
        file:write_file(File, urls2html(Urls)).
    
    bin2urls(Bin) ->  gather_urls(binary_to_list(Bin), []).
    
    urls2html(Urls) -> [h1("Urls"),make_list(Urls)]. 
        
    h1(Title) -> ["<h1>", Title, "</h1>\n"].
    
    make_list(L) ->
        ["<ul>\n",
         map(fun(I) -> ["<li>",I,"</li>\n"] end, L),
         "</ul>\n"]. 
    
    
    
    gather_urls("<a href" ++ T, L) ->
        {Url, T1} = collect_url_body(T, reverse("<a href")),
        gather_urls(T1, [Url|L]);
    gather_urls([_|T], L) ->
        gather_urls(T, L);
    gather_urls([], L) ->
        L.
    
    collect_url_body("</a>" ++ T, L) -> {reverse(L, "</a>"), T};
    collect_url_body([H|T], L)       -> collect_url_body(T, [H|L]);
    collect_url_body([], _)          -> {[],[]}.

๋žœ๋ค ์•ก์„ธ์Šค๋กœ ํŒŒ์ผ ์“ฐ๊ธฐ

    1> {ok, S} = file:open("...", [raw, write, binary]).
    2> file:pwrite(S, 10, <<"new">>).
    3> file:close(S).

๋””๋ ‰ํ„ฐ๋ฆฌ ์กฐ์ž‘

    1> cd("/home/joe/book/erlang/Book/code").
    2> file:list_dir(".").

ํŒŒ์ผ์— ๊ด€ํ•œ ์ •๋ณด ์ฐพ๊ธฐ

ํŒŒ์ผ ๋ณต์‚ฌํ•˜๊ณ  ์‚ญ์ œํ•˜๊ธฐ

  • file:copy(Source, Destination)
  • file:delete(File)

์žก๋™์‚ฌ๋‹ˆ

Find ์œ ํ‹ธ๋ฆฌํ‹ฐ

โš ๏ธ **GitHub.com Fallback** โš ๏ธ