Save Data to a File
From Erlang Community
(Difference between revisions)
| Revision as of 12:26, 20 September 2006 (edit) Dmitriid (Talk | contribs) m (Linked functions) ← Previous diff |
Current revision (14:22, 24 September 2006) (edit) (undo) Ayrnieu (Talk | contribs) (replace unnecessary reimplementation of file:write_file/2 with that function, and then add a tiny bit about saving consultable Erlang data structures, to make up for the blank page) |
||
| Line 5: | Line 5: | ||
| == Solution == | == Solution == | ||
| - | + | Where 'some data' is simply a list, you have this simple solution: | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| <code> | <code> | ||
| - | + | file:write_file(Path, Data). | |
| </code> | </code> | ||
| - | + | But if you want to save an Erlang data structure, to perhaps file:consult/1 later, you have this slightly more complex solution: | |
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | + | ||
| - | We further check for return codes from the [http://www.erlang.se/doc/doc-5.5/lib/stdlib-1.14/doc/html/io.html#fwrite/3 io:fwrite/3] function and never forget to close the file: | ||
| <code> | <code> | ||
| - | file: | + | file:write_file(Path,io_lib:fwrite("~p.\n",[Data])). |
| </code> | </code> | ||
| [[Category:CookBook]][[Category:FileRecipes]] | [[Category:CookBook]][[Category:FileRecipes]] | ||
Current revision
[edit] Problem
You want to save some data to a file.
[edit] Solution
Where 'some data' is simply a list, you have this simple solution:
file:write_file(Path, Data). |
But if you want to save an Erlang data structure, to perhaps file:consult/1 later, you have this slightly more complex solution:
file:write_file(Path,io_lib:fwrite("~p.\n",[Data])).
|

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

