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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shanmuga SundaramDirector of Software EngineeringCommented:
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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
Dagan HooverDeveloperCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.