tags:

views:

77

answers:

1

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 ) :

+4  A: 

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):

getValue() -> 1.

....
{ok, File} = file:open(integer_to_list(getValue()), [write]),

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.

womble
Spot on, ( except for a missing bracket ). Thanks - what makes it worse is I thought I tried that *sigh*...
Stephen Bailey
Whups, let's fix up that bracket, shall we...
womble