[英]Getting filename from IoDevice in Erlang
I have a function which tries to read from an open IoDevice. 我有一个试图从打开的IoDevice读取的函数。 In the case of {error, Reason} I want to print out the filename.
对于{error,Reason},我想打印出文件名。
How can I do that? 我怎样才能做到这一点?
Source code: 源代码:
read_file(IoDevice) ->
case read_file(IoDevice, []) of
{ok, OpCodes} ->
OpCodes;
{error, Reason} ->
io:format("Unable to read file ~s: ~s ~n", [File, Reason]),
[]
end.
An IoDevice
can be either a process ID or a file descriptor. IoDevice
可以是进程ID或文件描述符。 If it's a process ID, you can use file:pid2name/1
to obtain the filename: 如果是进程ID,则可以使用
file:pid2name/1
获取文件名:
1> {ok,IoDevice} = file:open("/tmp/x.erl", [read]).
{ok,<0.43.0>}
2> {ok, Filename} = file:pid2name(IoDevice).
{ok,"/tmp/x.erl"}
If the IoDevice
is a file descriptor, though, pid2name
won't work, and I don't know of a way to obtain the filename in that case. 但是,如果
IoDevice
是文件描述符,则pid2name
将不起作用,在这种情况下,我不知道获取文件名的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.