Solved

TFileStream

Posted on 2002-06-18
6
1,034 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
Technology Partners: 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

734 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