Avatar of boycoder
boycoder

asked on 

delphi read from text file

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
DelphiPascal

Avatar of undefined
Last Comment
boycoder
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image


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


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

Open in new window

Avatar of boycoder
boycoder

ASKER

Sorry mate i wasnt clear,

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


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);



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

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;
Its the end of the day, I'm a bit tired :)
Avatar of boycoder
boycoder

ASKER

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
Avatar of boycoder
boycoder

ASKER

sorry just read ur last part, ill give it a try.
Avatar of Mahdi78
Mahdi78
Flag of Algeria image

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

Avatar of boycoder
boycoder

ASKER

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);
ASKER CERTIFIED SOLUTION
Avatar of fromer
fromer

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of boycoder
boycoder

ASKER

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.
Avatar of boycoder
boycoder

ASKER

A
Delphi
Delphi

Delphi is the most powerful Object Pascal IDE and component library for cross-platform Native App Development with flexible Cloud services and broad IoT connectivity. It provides powerful VCL controls for Windows 10 and enables FMX development for Windows, Mac and Mobile. Delphi is your choice for ultrafast Enterprise Strong Development™. Look for increased memory for large projects, extended multi-monitor support, improved Object Inspector and much more. Delphi is 5x faster for development and deployment across multiple desktop, mobile, cloud and database platforms including 32-bit and 64-bit Windows 10.

60K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo