1 / 14

Erlang I/O Operations: Files, Terms, and Text

Learn how to perform input/output operations on Erlang files, including reading and writing Erlang terms and text files. Explore modules for file manipulation, file consultation, and line reading.

nilsen
Télécharger la présentation

Erlang I/O Operations: Files, Terms, and Text

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Science 312 Overview of Erlang IV Files (Chapter 16)

  2. Files • I/O of Erlang Terms • I/O of text • I/O of binaries • Modules: file, filename, io

  3. I/O of Erlang Terms • Reads in text as Erlang terms • Can consult entire file or read term by term

  4. Read All Terms: consult In the file data1.dat: {person, "joe", "armstrong", [{occupation, programmer}, {favoriteLanguage, erlang}]}. {cat, {name, "zorro"}, {owner, "joe"}}. 1> file:consult("data1.dat"). {ok, [{person, "joe", "armstrong", [{occupation, programmer}, {favoriteLanguage, erlang}]}., {cat, {name, "zorro"}, {owner, "joe"}}.]} Returns {error, Reason} if all is not well.

  5. Read One Term at a Time 1> {ok, S} = file:open("data1.dat", read). {ok,<0.36.0>} 2> io:read(S, ''). {person, "joe", "armstrong", [{occupation, programmer}, {favoriteLanguage, erlang}]} 3> io:read(S, ''). {cat, {name, "zorro"}, {owner, "joe"}} 4> io:read(S, ''). eof 5> file:close(S). ok -spec io:read(IoDevice, Prompt) -> {ok, Term} | {error,Why} | eof

  6. Define consult consult(File) -> case file:open(File, read) of {ok, S} -> Val = consult1(S), file:close(S), {ok, Val}; {error, Why} -> {error, Why} end. consult1(S) -> case io:read(S, '') of {ok, Term} -> [Term|consult1(S)]; eof -> []; Error -> Error end.

  7. Writing Terms to a File In lib_misc.erl: 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> lib_misc:unconsult("test1.dat", [{cats,["zorrow","daisy"]}, {weather,snowing}]). ok 2> file:consult("test1.dat"). {ok,[{cats,["zorrow","daisy"]},{weather,snowing}]}

  8. I/O of Text • Read line by line • Write line by line

  9. Read One Line at a Time 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

  10. Writing Lines of Text to a File -spec io:format(IoDevice, Format, Args) -> ok 1> {ok, S} = file:open("test2.dat", write). {ok,<0.62.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

  11. Writing Lines of Text to a File Format flags: ~n Newline character (system-independent) ~p Pretty-print the datum ~s String ~w An Erlang term Field widths and justification: Format Result ====== ====== io:format("|~10s|",["abc"]) | abc| io:format("|~-10s|",["abc"]) |abc | io:format("|~10.3.+s|",["abc"]) |+++++++abc| io:format("|~-10.10.+s|",["abc"]) |abc+++++++| io:format("|~10.7.+s|",["abc"]) |+++abc++++|

  12. Numbering Lines echo() -> InPathName = strip(io:get_line("Enter the input file name: "), right, $\n), OutPathName = strip(io:get_line("Enter the output file name: "), right, $\n), case InPathName == OutPathName of true -> io:format("Error: files must have different names~n"); false -> {ok, InputFile} = file:open(InPathName, read), {ok, OutputFile} = file:open(OutPathName, write), helper(InputFile, OutputFile, 1), file:close(OutputFile) end.

  13. Numbering Lines helper(InputFile, OutputFile, LineNumber) -> Line = io:get_line(InputFile, ''), case Line of eof -> done; _Anything -> io:format(OutputFile, "~4p ~s", [LineNumber, Line]), helper(InputFile, OutputFile, LineNumber + 1) end.

  14. For next time OTP A generic server Hot code swapping (Chapters 22-23)

More Related