Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.