【问题标题】:How do I write output to a text file without overriding the existing contents of the file?如何在不覆盖文件现有内容的情况下将输出写入文本文件?
【发布时间】:2013-03-14 11:48:55
【问题描述】:

我开发了这个函数,可以将数据从erlang 传输到.txt 文件:

exporttxt()->

F1 ="1",
F2 = "afif",
F3 = "kaled",

file:write_file("test.txt",[io_lib:format("~p\t~p\t~p~n",[F1,F2,F3])] ).

运行此函数后test.txt 包含这些值:

"1" "afif" "kaled"

但是当我将函数exporttxt() 中的F1F2F3 更改为:

F1 ="2"
F2 ="ahmed"
F3 = "alagi"

那么test.txt只包含这些值:

"2" "ahmed" "alagi"

我希望test.txt 包含:

"1"  "afif"  "kaled"
"2"  "ahmed" "alagi"

问题是在每次执行函数时它都会记录新数据 并且test.txt中的旧数据被删除。

如何在不覆盖现有数据的情况下将新数据写入test.txt

【问题讨论】:

标签: file-io erlang


【解决方案1】:

在这一点上使用file:write_file/3

第三个参数是Modes。读取可能模式的列表| write | append | exclusive | raw | binary | {delayed_write, Size, Delay} | delayed_write | {read_ahead, Size} | read_ahead | compressed | {encoding, Encoding}append 模式满足您的需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-21
    • 2016-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    相关资源
    最近更新 更多