140 likes | 154 Views
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.
E N D
Computer Science 312 Overview of Erlang IV Files (Chapter 16)
Files • I/O of Erlang Terms • I/O of text • I/O of binaries • Modules: file, filename, io
I/O of Erlang Terms • Reads in text as Erlang terms • Can consult entire file or read term by term
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.
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
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.
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}]}
I/O of Text • Read line by line • Write line by line
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
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
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++++|
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.
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.
For next time OTP A generic server Hot code swapping (Chapters 22-23)