How to read Two words from a line (external file) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to read Two words from a line (external file)

SlladSllad Posts: 19Member
What the subject rly says. I want to make my pascal programm read something from an external text file. I want to Read TWO DIFFERENT values, both found in the same line of the external file. For example the input file is

105 77

and i want to use 105 and 77 as variables. How to do that?


Thnx in advance...

Comments

  • zibadianzibadian Posts: 6,349Member
    : What the subject rly says. I want to make my pascal programm read something from an external text file. I want to Read TWO DIFFERENT values, both found in the same line of the external file. For example the input file is
    :
    : 105 77
    :
    : and i want to use 105 and 77 as variables. How to do that?
    :
    :
    : Thnx in advance...
    :
    If the two parts are always integers, then the solution is simple: readln() with a file and 2 integer-typed variables will implicitly split the line into parts using the space and tab characters.
    In your case the would looks like this
    [code]
    var
    f: text;
    var1, var2: integer;
    begin
    ...
    readln(f, var1, var2);
    ...
    end;
    [/code]
    Obviously you can change the names and need to add the rest of the code.

    If the two parts are strings, then you need to read the line as a single string and split them as needed. Here is a function, which can split strings using a single characters as divider: http://www.codepedia.com/1/StringSplit
Sign In or Register to comment.