Read

I was recently trying to find out the difference between 'Readln' and 'Read.' I looked up 'Read' in the TP index and try to run their sample code, but TP always told me it expected an '.' when it came to read. This seems to be the case whenever I attempt to use 'Read' in a program.
Anyone know the difference btw 'Read' and 'Readln.'

Comments

  • : I was recently trying to find out the difference between 'Readln' and 'Read.' I looked up 'Read' in the TP index and try to run their sample code, but TP always told me it expected an '.' when it came to read. This seems to be the case whenever I attempt to use 'Read' in a program.
    : Anyone know the difference btw 'Read' and 'Readln.'
    :
    When you use Read() and Readln() to get input from the user, then there is no difference. When reading from files there is a big difference however. Readln() can only be used on text files and automatically jumps to the next line, while Read() can be used on all typed file types and doesn't automatically jump to the next line if used on a text file.
    Here are some examples:
    [code]
    { This code shows the first 2 lines of a text file }
    var
    f: text;
    s: string;
    begin
    Assign(f, 'somefile.txt');
    Reset(f);
    Readln(f, s);
    Writeln(s);
    Readln(f, s);
    Writeln(s);
    Close(f);
    end;
    [/code]
    [code]
    { This code shows only the first line of a text file }
    var
    f: text;
    s: string;
    begin
    Assign(f, 'somefile.txt');
    Reset(f);
    Read(f, s); { Doesn't advance to the next line }
    Writeln(s);
    Read(f, s); { Tries to continue reading the 1st line }
    Writeln(s);
    Close(f);
    end;
    [/code]
    [code]
    var
    f: text of char;
    ch: char;
    begin
    Assign(f, 'somefile.txt');
    Reset(f);
    Read(f, ch); { Reads the first character of the file }
    Writeln(ch);
    Readln(f, ch); { Gives a compilation error }
    Writeln(ch);
    Close(f);
    end;
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories

In this Discussion