Solved

TFileStream

Posted on 2002-06-18
6
1,024 Views
Last Modified: 2010-05-18
HiCan i read with TFileStream  line by line
(every Line can be in different length)

Example will be wellcome
thank u
Asi
0
Comment
Question by:asi
6 Comments
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 7088977
The easiest way to read a text file is to use a TStringList.

var
  List: TStringList;
begin
  List := TStringList.Create;
  List.LoadFromFile(FileName);
  // do something with the lsit
  List.Free;
end;
0
 

Author Comment

by:asi
ID: 7089401
I need to read a very  big file name.
TStringList is not the way
0
 
LVL 6

Expert Comment

by:edey
ID: 7090054
to do it with a stream you have to read the file char by char until you hit a #10. It's also possible to use a text file. my help (D6) contained this example:

AssignFile, OpenDialog, Readln, CloseFile Example:
var

  F: TextFile;
  S: string;
begin
  if OpenDialog1.Execute then            { Display Open dialog box }
  begin
    AssignFile(F, OpenDialog1.FileName); { File selected in dialog }
    Reset(F);
    Readln(F, S);                        { Read first line of file }
    Edit1.Text := S;                     { Put string in a TEdit control }
    CloseFile(F);
  end;
end;

GL
Mike
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:DragonSlayer
ID: 7091548
open using FileStream, then load stream data as needed, into TStringStream?
0
 

Accepted Solution

by:
philcm earned 25 total points
ID: 7108132
You'll need to create a command for your filestream!  Ideal thing is to creat a new Object that inherits TFileStream and add the funtion ReadFSLn.  Either way... I've attached code that should help you.  

ReadFSLn will read 1 one and return it has a string. Note that there is 3 things (According to ReadLN in Delphi) that determins a line.  

Hope this help you

//------------------------------------------------------------------------------
//Possible Line Combo  (LF, CR+LF, CR+EOF)
//------------------------------------------------------------------------------
function TForm1.ReadFSLn(FS: TFileStream): String;
var
  Buffer : Char;
  LastChr : Char;
begin
  //Init!
  Result := '';
  Buffer := #0;
  LastChr := #0;
  //Loop till you find the end of a line
  while FS.Position < FS.Size do //Make sure never to pass the EOF
  begin
    FS.Read(Buffer,1);
    Result := Result + Buffer;
    if (Buffer = #10) or ((LastChr = #13) and (Buffer=#10)) or (Buffer=#0) then
      exit; //Line found exit!
    LastChr := Buffer;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  FS : TFileStream;
begin
  FS := TFileStream.Create('D:\NewLog.log',fmOpenRead);
  try
    while FS.Position < FS.Size do
      Memo1.Lines.Add( ReadFSLn(FS) );

  finally
    FreeAndNil(FS);
  end;

end;
0
 

Expert Comment

by:junsom
ID: 23677840
thanks so much
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to disable mouse scroll in a combo box field within a Delphi cxGrid 4 158
Printing problem 2 101
Mobile Keyboard covers the display of the TMemo 3 96
Delphi 2 69
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question