Solved

TFileStream

Posted on 2002-06-18
6
1,013 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Master Detail with TADODataset 4 97
Delphi XE2 application frozen on Windows 10 10 231
Magic Software info 18 105
Dynamically Created Query 3 13
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now