Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7847793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:00:14+00:00 2026-06-02T18:00:14+00:00

I am trying to read an integer value from a simple text file. Row

  • 0

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}
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:00:14+00:00Added an answer on June 2, 2026 at 6:00 pm

    You must first use file:read_line/1 to read a line of text, and use re:split/2 to get a list of strings containing numbers. Then use the list_to_integer BIF to get integers.

    Here’s an example (surely there is a better solution):

    #!/usr/bin/env escript
    %% -*- erlang -*-
    
    main([Filename]) ->
        {ok, Device} = file:open(Filename, [read]),
        read_integers(Device).
    
    read_integers(Device) ->
        case file:read_line(Device) of
        eof ->
            ok;
        {ok, Line} ->
            % Strip the trailing \n (ASCII 10)
            StrNumbers = re:split(string:strip(Line, right, 10), "\s+", [notempty]),
            [N1, N2, N3, N4] = lists:map(fun erlang:list_to_integer/1, 
                         lists:map(fun erlang:binary_to_list/1, 
                               StrNumbers)), 
            io:format("~w~n", [{N1, N2, N3, N4}]),
            read_integers(Device)
        end.
    

    (EDIT)

    I found a somewhat simpler solution that uses io:fread to read formatted input. It works well in your case but fails badly when the file is badly constructed.

    #!/usr/bin/env escript
    %% -*- erlang -*-
    
    main([Filename]) ->
        {ok, Device} = file:open(Filename, [read]),
        io:format("~w~n", [read_integers(Device)]).
    
    read_integers(Device) ->
        read_integers(Device, []).
    
    read_integers(Device, Acc) ->
        case io:fread(Device, [], "~d~d~d~d") of
        eof ->
            lists:reverse(Acc);
        {ok, [D1, D2, D3, D4]} ->
            read_integers(Device, [{D1, D2, D3, D4} | Acc]);
        {error, What} ->
            io:format("io:fread error: ~w~n", [What]),
            read_integers(Device, Acc)
        end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to check if a value I read from a text file
I'm trying to read user input of integer. (like cin >> nInput; in C++)
i'm trying to read long types from a text file with using readLine() method
I am trying to read Data from a Text file & storing it inside
I am trying to read in 10 signed integers from a file into an
I'm trying Perl on Mac. I have to read an RTF text file. the
I am trying to read the input from a keyboard which i will use
i am trying to read file from a stream. and i am using stream.read
I am trying to read the day value from native calendar of blackberry,the value
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.