text file processing

i'm trying to process a text file line by line. are there any good routines out there to do this?
LVL 1
Michael SterlingWeb Applications DeveloperAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Shanmuga SundaramConnect With a Mentor Director of Software EngineeringCommented:
0
 
Shanmuga SundaramDirector of Software EngineeringCommented:
0
 
rfwoolfCommented:
var
  sl: TStringList;
You can load it into a Stringlist, but this only handy for "small" text file. If you have extremely large text files then you can look at using streams and other fanciful stuff.

example:
var
MyStringList : TStringlist;
i : integer;
begin
try
  MyStringList := TStringList.Create;
  MyStringList.LoadFromFile('C:\MyTextFile.txt');
  //Read each line
  for i := 0 to MyStringlist.lines.count - 1 do
   begin
       Showmessage('Here's a line: ' + MyStringList.Lines[i]);
  end;

finally
  FreeAndNil(MyStringList)
end
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
developmentguruPresidentCommented:
What kind of processing?  What line format?  There are an infinite number of types of processing that can be done on files, if you want us to be more help we will need more ingormation.
0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
developmentguru:
 
I need to read a text file, line by line to the end of it. i'll be parsing the lines once i've read them in, so i imagine i'll need to read them into a string. does that help?
0
 
developmentguruPresidentCommented:
 What size file might you expect to see (number of lines - guess large)?  What type of processing is done on each line?  Do you need to worry about the format of the file (might it be unicode?, single byte characters?  what type of carriage return line feed combinations might the routine run into?).  Some text files use a #13 and some use a #13#10 for end of line.  Depending on the origin of the text file how you handle it can vary greatly.  If you are generating the file then you will know the format of the text and the processing can be simplified.  If the file is relatively short then you can simplify further.  If, on the other hand, the file could come from any of 13 countries and could be 4GB plus then you need to be able to build that processing capability into the code.
0
 
ThievingSixCommented:
This might help:

1) Reads from a text file.
2) Splits it up after each line and puts into a TStringList
3) Example shows it into the TMemo
type
  TFileString = AnsiString;
  DChar = String[2];
 
function ReadTextFile(sTextFile: String) : TFileString;
var
  fsText : TFileStream;
begin
  fsText := TFileStream.create(sTextFile,fmOpenRead);
  try
    SetLength(Result,fsText.Size);
    fsText.Read(PAnsiString(Result)^,fsText.Size);
  finally
    fsText.Free;
  end;
end;
 
function GetNextToken(Const sInput: String; dcSeparator: Dchar; var iStartPos: Integer): String;
var
  iIndex : Integer;
begin
  Result := '';
  While ((sInput[iStartPos] + sInput[iStartPos + 1]) = dcSeparator) And (iStartPos <= Length(sInput))do
    begin
    Inc(iStartPos);
  end;
  If iStartPos > Length(sInput) then Exit;
  iIndex := iStartPos;
  While ((sInput[iIndex] + sInput[iIndex + 1]) <> dcSeparator) And (iIndex <= Length(sInput)) Do
    begin
    Inc(iIndex);
  end;
  Result := Copy(sInput,iStartPos,iIndex - iStartPos);
  iStartPos := iIndex + 2;
end;
 
function FileStringToList(const sInput: TFileString): TStringList;
var
  iStart : Integer;
  dcSeparator : DChar;
begin
  Result := TStringList.Create;
  Assert(Result <> nil);
  iStart := 1;
  dcSeparator := #13#10;
  While iStart <= Length(sInput) Do
    begin
    Result.Add(GetNextToken(sInput, dcSeparator, iStart));
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
  slLineByLine : TStringList;
begin
  slLineByLine := FileStringToList(ReadTextFile('C:\1.txt'));
  Memo1.Lines := slLineByLine;
  slLineByLine.Free;
end;

Open in new window

0
 
Michael SterlingWeb Applications DeveloperAuthor Commented:
Thanks, all the anwers were good but this one was exaclty what my particular situation needed. (the second link, i never even clicked the first one).
0
All Courses

From novice to tech pro — start learning today.