Mega Code Archive

 
Categories / Delphi / Examples
 

Cdf

example code for parsing TAB-delimited text file (minor adjustments necessary for comma-delimited...) Date sent: Fri, 02 Apr 1999 10:30:27 -0500 From: Stephen Posey <slposey@concentric.net> To: Delphi@Kyler.com Subject: Re: Loading tab-delimited files? Send reply to: Delphi@Kyler.com dB_Consultancy wrote: > > On Fri, 02 Apr 1999 09:49:30 +0200, Thomas Sundberg wrote: > > >Explore the datapump utility that comes with Delphi. Or do it > >yourself with readln in a while loop (while not eof), parse the > >line for tab and brake it apart and store each part in the db. > > Sorry, But I never used files in applications (only registry for > settings). > Could you be so nice to sent me example source code? > How to read a tab-delimited textfile in Delphi? > (it will have 4-5 colums) > > I will aperciate this very much... Hmmmm, I'd recommend studying basic Pascal a bit before you attempt much more sophisticated Delphi work. There's plenty of good texts out there for that; though I'd recommend a book that deals specifically with Borland's particular dialect. Anyways, here's some basic code to read a tab delimited text file: -------------------->8 cut here 8<-------------------- procedure ParseLine( TheLine: string; List: TStrings ) ; // split a tab delimited line of text into // substrings in List const TAB = #9 ; // standard ASCII/ANSI char code for Tab var P: longint ; // where is next Tab found? Temp: string ; // remainder of string begin List.Clear ; // clear last set Temp := TheLine ; // save line P := Pos( TAB, Temp ) ; // find first Tab char if P = 0 then // account for no Tabs: P := Length( Temp ) ; // add whole string to List while P > 0 do begin List.Add( Copy( Temp, 1, P - 1 )) ; // append substring to list Temp := Copy( Temp, P + 1, Length( Temp ) - P ) ; // shorten Temp P := Pos( TAB, Temp ) ; // find next Tab char end ; end ; procedure DoStuffWithList( LineList: TStrings ) ; var i: integer ; begin for i := 0 to LineList.Count - 1 do { here's where you work with the parsed items from a line } end ; var TheFile: TextFile ; // tab delimited text file ALine: string ; // A line from the file TheList: TStringList ; // List to store parsed items begin TheList := TStringList.Create ; // associate file with disk file AssignFile( TheFile, 'FILENAME.TXT' ) ; // prepare file for reading Reset( TheFile ) ; while not EOF( TheFile ) do // as long as there's more file begin ReadLn( TheFile, ALine ) ; // get a line ParseLine( ALine, TheList ) ; // split into substrings DoStuffWithList( TheList ) ; // use the substrings end ; // cleanup CloseFile( TheFile ) ; TheList.Free ; end ; -------------------->8 cut here 8<-------------------- Note that this code is untested, it does no error checking, and the parsing is very simple minded (it will likely have trouble with multiple tabs); but this should get you started. HTH Stephen Posey slposey@concentric.net **************************************************** If you don't want to see any more of these messages, send a message to: MajorDomo@Kyler.com with a message of: UNSUBSCRIBE delphi