0

Given a file input.txt, which consists of a number of elements in arrays and elements in the arrays, I should read the data and copy it to arrays in Eiffel. For example, for

3 
3 4 5
2 3 1

I should get len = 3, a1 = {3,4,5}, a2 = {2,3,1}.

I have tried the following but it was not successful

take_input
        -- Read user's input.
    local
        input: PLAIN_TEXT_FILE
        output: PLAIN_TEXT_FILE
        itr: INTEGER
    do
        create input.make_open_read ("input.txt")
        input.read_integer
        len := input.last_integer
        create output.make_open_write ("output.txt")
        create num.make_filled (0, 1, len)
        create rem.make_filled (0, 1, len)
        from
            input.start
            input.read_integer
            itr := 0
        until
            input.off
        loop
            itr := itr + 1
            if itr <= len then
                num [itr] := input.last_integer
                input.read_integer
            else
                rem [itr - len] := input.last_integer
                input.read_integer
            end
        end
        input.close
    end

Here Is there any way I can continously read the inputs rather than again starting from the Begginning of the file?

Akash Tadwai
  • 100
  • 1
  • 9
  • There are several questions that may help you find the correct solution: 1) Why the code opens file `input` instead of `input.txt`? 2) Do you need an output file? 3) Wouldn't it be easier to have 2 loops for 2 arrays instead of one loop with a conditional instruction? 4) Why do you restart reading the input file from the beginning (`input.start`) right before the loop? – Alexander Kogtenkov Nov 15 '19 at 07:45
  • @AlexanderKogtenkov Yes, Its input.txt, I have mistyped it here, Yes I need an output file, 3) I have tried that way too! For the 4th question What's the syntax for continuing reading file from middle of input.txt file – Akash Tadwai Nov 15 '19 at 11:42

1 Answers1

0

There is no need to go to the beginning of the file after reading the number of elements. Therefore, removing input.start right after from will do the trick.

As a sanity check (in case it matters for your program), it makes sense to test whether len is positive before starting the loop.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35