erlang,将整数值写入文件
我想将整数值打印到文件中。我可以将字符串值写入文件,但是当我尝试写入整数值时,会出现错误:
%this works fine
{ok, F}=file:open("bff.txt", [read,write]),
Val="howdy",
file:write(F,Val).
%this gets compiled, but results in error {error, badarg} while executing
{ok, F}=file:open("bff.txt", [read,write]),
Val=23424,
file:write(F,Val).
有什么建议吗?
实际上,我想为 Web 服务器编写一个基准测试代码,我需要将所有时间值和请求数写入输出文件,然后我将使用它通过 gnuplot 绘制图形。
I want to print integer values to a file. I am able to write string values to the file, but when I try to write an integer value it gives an error:
%this works fine
{ok, F}=file:open("bff.txt", [read,write]),
Val="howdy",
file:write(F,Val).
%this gets compiled, but results in error {error, badarg} while executing
{ok, F}=file:open("bff.txt", [read,write]),
Val=23424,
file:write(F,Val).
Any suggestions?
Actually I want to write a benchmarking code for a web server and I need to write all the values of times and no of requests to an output file, and then I'll use it to plot graph with gnuplot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
integer_to_list/1
将整数转换为file:write/2
的列表。Use
integer_to_list/1
to convert integers to a list forfile:write/2
.这是因为
file:write
只能输出字符串。另一种方法是使用 io 模块中的函数,这些函数也可以处理文件。所以 io:write(File, Val) 将会起作用。您也可以使用格式化 io 函数io:format
。这实际上取决于您希望如何格式化数据以及如何读取它们,如果您打算读取它们,仅使用 io:write 写入整数将不会很有用。This is because
file:write
only can output strings. An alternative is to use functions in theio
module which also work on files. Soio:write(File, Val)
will work. You can alternatively use formatted io functionsio:format
. It really depends how you wish to format the data and how they are to be read, just writing integers withio:write
will not be very useful if you intend to read them.您可以使用 term_to_binary 和 binary_to_term:
You may use term_to_binary and binary_to_term: