Solved

TFileStream

Posted on 2002-06-18
6
1,040 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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