Howdy, Stranger!

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

Categories

How do I read from a .txt file?

SolusSolus Member Posts: 2
Hello. Can someone tell me how to read ALL of a .txt file and write it to a memobox?

So far I have only succeeded in writing the first line. As soon as I use an enter, nothing below that is read.

Can someone help?

Thankyou in Advance
Solus

Comments

  • zibadianzibadian Member Posts: 6,349
    [b][red]This message was edited by zibadian at 2003-5-10 1:33:15[/red][/b][hr]
    : Hello. Can someone tell me how to read ALL of a .txt file and write it to a memobox?
    :
    : So far I have only succeeded in writing the first line. As soon as I use an enter, nothing below that is read.
    :
    : Can someone help?
    :
    : Thankyou in Advance
    : Solus
    :
    The Lines property of a TMemo are a TStrings object themselves. This class has a LoadFromFile() method, which handles simple textfiles. You can call this using a single line:
    [code]
    Memo1.Lines.LoadFromFile('myfile.txt');
    [/code]

    Hint: If stuck with such a problem, check the help files for likely properties and methods. If a property is another object, check its properties and methods also. Look for logical names, in this case anything with Load or Read in it.
  • SolusSolus Member Posts: 2
    Is this the only way to make a memo only display a specific line.

    [code]Memo1.Lines.LoadFromFile('memo.txt');
    Memo1.Text:= Memo1.Lines[5];
    [/code]

    I am asking to find out if there is a faster way. Perhaps a way to do it all in the first line.

    This is just out of curiousity. I am wondering because say there was an error or the computer slowed down or froze on the first line of code. The user would then see all of the text document instead of just that line.

    It is unlikely that it would happen, but it's good to know other methods.

    Thanks in Advance.
  • zibadianzibadian Member Posts: 6,349
    : Is this the only way to make a memo only display a specific line.
    :
    : [code]Memo1.Lines.LoadFromFile('memo.txt');
    : Memo1.Text:= Memo1.Lines[5];
    : [/code]
    :
    : I am asking to find out if there is a faster way. Perhaps a way to do it all in the first line.
    :
    : This is just out of curiousity. I am wondering because say there was an error or the computer slowed down or froze on the first line of code. The user would then see all of the text document instead of just that line.
    :
    : It is unlikely that it would happen, but it's good to know other methods.
    :
    : Thanks in Advance.
    :
    You could read the file into a temporary TStringList object, and then get the 6th line from there and copy it into the memo. Here is the code for that:
    [code]
    var
    S: TStringList;
    begin
    S := TStringList.Create;
    try
    S.LoadFromFile();
    Memo1.Text := S[5];
    finally
    S.Free;
    end;
    end;
    [/code]
    This is quite a slow way to show a single line several times. Better is to make the stringlist a field of your form. Then you only need to read the file once and get any line at any time in the rest of your code.
    Here is part of such code.
    [code]
    TForm1 = class(TForm)
    ...
    private
    FMemoryMemo: TSTringList;
    ...
    public
    ...
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    FMemoryMemo := TStringList.Create;
    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    FMemoryMemo.Free;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    Memo1.Text := FMemoryMemo[5];
    end;
    [/code]
Sign In or Register to comment.