delphi read from text file

boycoder
boycoder used Ask the Experts™
on
How can i read from a text file..


loacted in C:\Windows\test.txt

Could you make it a function please so i can call it.
thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ephraim WangoyaSoftware Engineer

Commented:

Where do you want to load the file contents?

Lets say you want them in a TMemo
function LoadFromFile(const AFileName: string; AMemo: TMemo);
begin
  AMemo.Lines.Clear;
  AMemo.Lines.LoadFromFile(AFileName);
end;

Open in new window

Ephraim WangoyaSoftware Engineer

Commented:

If its a TRichEdit
function LoadFromFile(const AFileName: string; ARichEdit: TRichEdit);
begin
  ARichEdit.Lines.Clear;
  ARichEdit.Lines.LoadFromFile(AFileName);
end;

Open in new window

Author

Commented:
Sorry mate i wasnt clear,

I want to get the string of the textfile.. so i can send it back to my client.

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Ephraim WangoyaSoftware Engineer

Commented:

So basically we are using descendants of TStrings which is the Lines property
You can also load the contents directly to a TStringList if you want

function LoadFromFile(const AFileName: string; AStrings: TStrings);
begin
  AStrings.Clear;
  AStrings.LoadFromFile(AFileName);
end;

and calling the methods
LoadFromFile('C:\Windows\test.txt', Memo1);

or
LoadFromFile('C:\Windows\test.txt', RichEdit1);

or
var List: TStringList;
LoadFromFile('C:\Windows\test.txt', List);


Ephraim WangoyaSoftware Engineer

Commented:

In that case, use a StringList



function LoadFromFile(const AFileName: string): string;
var 
  List: TStringList;
begin
  List := TStringList.Create;
  try
    LoadFromFile('C:\Windows\test.txt', List);
    Result := List.Text;
  finally
    FreeAndNil(List);
  end;
end;

Open in new window

Ephraim WangoyaSoftware Engineer

Commented:
sorry mate, that was no good

here is the correct one

function LoadFromFile(const AFileName: string): string;
var
  List: TStringList;
begin
  List := TStringList.Create;
  try
    List.LoadFromFile(AFileName);
    Result := List.Text;
  finally
    FreeAndNil(List);
  end;
end;
Ephraim WangoyaSoftware Engineer

Commented:
Its the end of the day, I'm a bit tired :)

Author

Commented:
I understand, but its not exactly what i want mate.
What iam trying to do is get the text inside a text file, and send it back to my client.

i call the functions and send them like this:

 SENDCOMMAND(COMMAND_READ,(GetIpAddress + CRLF),Socket);
or
SENDCOMMAND(COMMAND_READ,(edata + CRLF),Socket);

So, ull see that i need the function type in there so i can send it back, wheni recieve it on my client end it will be
listbox1.items:=(edata);

with edata being the string. ESTRING

Author

Commented:
sorry just read ur last part, ill give it a try.

Commented:
Drop TMemo, TButton and use this



procedure TForm1.Button1Click(Sender: TObject);
var
  MyFile:TextFile;
  Txt: string;
begin

  AssignFile(MyFile,'C:\Windows\test.txt');
  Reset(MyFile);

  while not EOF(MyFile) do
  begin
    Readln(MyFile,Txt);
    Memo1.Lines.Add(Txt)
  end;

  CloseFile(MyFile);

end;

Open in new window

Author

Commented:
its a formless form on the serverside.
however, i get the error cannot open file.

function LoadFromFile(const AFileName: string): string;
var
  List: TStringList;
begin
  List := TStringList.Create;
  try
    List.LoadFromFile(AFileName);
    Result := List.Text;
  finally
    FreeAndNil(List);
  end;
end;




//sending//

     COMMAND_READ:
        begin
        LoadFromFile('C:\Windows\pass.txt') ;

        SENDCOMMAND(COMMAND_READ,(LoadFromFile(edata) + CRLF),Socket);

//

what am i doingwrong?


btw, i recieve in client..

showmessage(edata);
Commented:
//sending//
 COMMAND_READ:
        begin
        edata := LoadFromFile('C:\Windows\pass.txt') ;

        SENDCOMMAND(COMMAND_READ,(edata + CRLF),Socket);

i think you are trying to do this?

Author

Commented:
BINGO my friend, bingo. Thanks to all who helped, and thanks for nailiing it fromer.
ewangoya:, cheers mate for getting me started on that right way.

Author

Commented:
A

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial