I am trying to read an integer value from a simple text file.
Row in my input file looks like this:
1 4 15 43
2 4 12 33
... (many rows of 4 integers)
I have opened the file in the following way:
{ok, IoDev} = file:open("blah.txt",[read])
But only thing that I manage to read are bytes with all the functions available.
What I finally want to get from file are tuples of integers.
{1 4 15 43}
{2 4 12 33}
...
You must first use
file:read_line/1to read a line of text, and usere:split/2to get a list of strings containing numbers. Then use thelist_to_integerBIF to get integers.Here’s an example (surely there is a better solution):
(EDIT)
I found a somewhat simpler solution that uses
io:freadto read formatted input. It works well in your case but fails badly when the file is badly constructed.