Erlang 构建动态文件名
我正在尝试创建一个文件,其名称基于函数中的整数值,显然下面不起作用,但给了你这样的想法:
getValue() -> 1.
createFile() ->
{ok, File} = file:open( getValue(), [write]),
io:format(File,"Test~n"),
file:close(File).
这应该很简单,即使 Erlangs 缺乏对字符串的支持,所以我必须是遗漏了一些明显的东西(就像新事物的代价一样):
I am trying to create a file with a name based on an integer value from a function, clearly below does not work but gives you the idea :
getValue() -> 1.
createFile() ->
{ok, File} = file:open( getValue(), [write]),
io:format(File,"Test~n"),
file:close(File).
This ought to be simple, even with Erlangs lack of support for strings, so I must just be missing something obvious ( as is the price of being new to something ) :
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您只想打开一个名为“1”的文件,那么您可以使用
integer_to_list/1
来执行此操作(因为字符串只是字符 ASCII 值的整数列表) :如果您想根据
getValue/0
中的值创建文件名,则适用相同的原则,但只需将多个列表粘合在一起即可创建文件名。If you just want to open a file whose name is "1", then you can use
integer_to_list/1
to do that (since a string is just a list of integers for the ASCII values of the characters):If you're wanting to create a filename based on the value from
getValue/0
, then the same principle applies, but you just create your filename from gluing several lists together.