:ets.tab2file badarg

Granted this is more of an Erlang question – why is this producing a badarg for those of you that have tried to write your ets tables to files? The web and ets docs aren’t explaining it for me. Thanks

http://erldocs.com/19.0/stdlib/ets.html#tab2file/2

$ iex
Erlang/OTP 19 [erts-8.1] [source] [64-bit] [smp:2:2] [async-threads:10] [kernel-poll:false]

Interactive Elixir (1.3.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> t = :ets.new(:the_table, [:bag, :named_table, :public])
:the_table
iex(2)> :ets.insert(t, {:x, "xylophone"})         
true
iex(3)> :ets.tab2list(t)
[x: "xylophone"]
iex(4)> :ets.tab2file(t, "foo.txt")
{:error, {:badarg, [file: "foo.txt"]}}
iex(5)> :ets.tab2file(t, "foo")    
{:error, {:badarg, [file: "foo"]}}
iex(6)> :ets.tab2file(t, :filename.absname("foo"))                  
{:error, {:badarg, [file: "/home/bibek/Workspace/foo"]}}
iex(7)> File.touch("foo.txt")
:ok

It’s almost certainly expecting a charlist instead of a binary of the filename.

:ets.tab2list(t, 'foo.txt')
1 Like

That’s my first thought too. :joy: . This always trips me up when working with Erlang in Elixir.

Thank you Ben(s). Yes it expects a charlist. Surprised :filename.absname didn’t do the trick. Thanks for saving me from extra head-butting time.

Since Jose already soft-soft-deprecated the use of single-quotes, I do think the community should move forward and only use the sigil from now on.

Using the sigil it were:

:ets.tab2list(t, ~c[foo.txt])
1 Like