Computer Science 312 Overview of Erlang IV Files

  • Slides: 14
Download presentation
Computer Science 312 Overview of Erlang IV Files (Chapter 16)

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

Files • I/O of Erlang Terms • I/O of text • I/O of binaries

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

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 data 1. dat: {person, "joe", "armstrong", [{occupation,

Read All Terms: consult In the file data 1. dat: {person, "joe", "armstrong", [{occupation, programmer}, {favorite. Language, erlang}]}. {cat, {name, "zorro"}, {owner, "joe"}}. 1> file: consult("data 1. dat"). {ok, [{person, "joe", "armstrong", [{occupation, programmer}, {favorite. Language, 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("data 1. dat",

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

Define consult(File) -> case file: open(File, read) of {ok, S} -> Val = consult

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

Writing Terms to a File In lib_misc. erl: unconsult(File, L) -> {ok, S} =

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("test 1. dat", [{cats, ["zorrow", "daisy"]}, {weather, snowing}]). ok 2> file: consult("test 1. dat"). {ok, [{cats, ["zorrow", "daisy"]}, {weather, snowing}]}

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

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

Read One Line at a Time 1> {ok, S} = file: open("data 1. dat",

Read One Line at a Time 1> {ok, S} = file: open("data 1. 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 {favorite. Language, 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(Io. Device, Format, Args) ->

Writing Lines of Text to a File -spec io: format(Io. Device, Format, Args) -> ok 1> {ok, S} = file: open("test 2. 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

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 ====== io: format("|~10 s|", ["abc"]) io: format("|~-10 s|", ["abc"]) io: format("|~10. 3. +s|", ["abc"]) io: format("|~-10. +s|", ["abc"]) io: format("|~10. 7. +s|", ["abc"]) Result ====== | abc| |abc | |+++++++abc| |abc+++++++| |+++abc++++|

Numbering Lines echo() -> In. Path. Name = strip(io: get_line("Enter the input file name:

Numbering Lines echo() -> In. Path. Name = strip(io: get_line("Enter the input file name: "), right, $n), Out. Path. Name = strip(io: get_line("Enter the output file name: "), right, $n), case In. Path. Name == Out. Path. Name of true -> io: format("Error: files must have different names~n"); false -> {ok, Input. File} = file: open(In. Path. Name, read), {ok, Output. File} = file: open(Out. Path. Name, write), helper(Input. File, Output. File, 1), file: close(Output. File) end.

Numbering Lines helper(Input. File, Output. File, Line. Number) -> Line = io: get_line(Input. File,

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

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

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