reading text file into array of string

Please discuss general Delphi programming topics here.

reading text file into array of string

Postby mathgod » January 10th, 2009, 8:37 pm

I should know how to do this but I can't make it work. The code snippet that I used to have used TFileStream inside a try finally block. Carriage return (#13#10) was detected and the loop ran until end of file.

This is what I need. I need to read a text file line for line and place each line into a cell of a dynamic array of string. The array is dynamic to accommodate text files of varying number of lines.

The program I am working on is to read a user written script and process the instructions in that script. My idea of reading a text file may be inefficient so I am open to better ideas for script reading / processing.

Don
User avatar
mathgod
Junior Member
Junior Member
 
Posts: 35
Joined: December 15th, 2007, 4:36 pm
Location: Middle of New Mexico, USA

Re: reading text file into array of string

Postby Kambiz » January 11th, 2009, 10:06 am

For having script line by line, I suggest using TStringList.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm

Re: reading text file into array of string

Postby mathgod » January 11th, 2009, 4:43 pm

Thanks, Kambiz. This is what I did... and it works great.
Code: Select all
Procedure TForm1.RunScript1Click(Sender: TObject);
    var
      Lines: TStringList;
      i, DelimPos, paramVal: Integer;
      M1, M2, S, imgFunct: String;
    begin
      if OpenTextFileDialog1.Execute then
         begin
           Lines := TStringList.Create;
           try
             Lines.LoadFromFile(OpenTextFileDialog1.FileName);
             for i := 0 to Lines.Count-1 do
               begin
                 S := Lines[i];
                 DelimPos := AnsiPos(',', S);
                 if DelimPos > 0 then
                    begin
                 M1:= Copy(S, 1, DelimPos - 1);
                 M2:= Copy(S, DelimPos + 1, MaxInt);
                 imgFunct:= AnsiLowerCase(M1);
                 paramVal:= StrtoInt(M2);
           if imgFunct = 'sepia' then image1.Picture.Graphic:= bmpsepia(image1.picture.bitmap, paramVal);
           if imgFunct = 'posterize' then image1.Picture.Graphic:= bmpposterize(image1.picture.bitmap, paramVal);
           if imgFunct = 'solarize' then image1.Picture.Graphic:= bmpsolarize(image1.picture.bitmap, paramVal);
           if imgFunct = 'relief' then image1.Picture.Graphic:= bmprelief(image1.picture.bitmap, paramVal);
           if imgFunct = 'emboss' then image1.Picture.Graphic:= bmpemboss(image1.picture.bitmap, paramVal);
                    end else
                 imgFunct:= AnsiLowerCase(S);
           if imgFunct = 'flaxen' then image1.Picture.Graphic:= bmpflaxen(image1.picture.bitmap);
           if imgFunct = 'negative' then image1.Picture.Graphic:= bmpnegative(image1.picture.bitmap);
           if imgFunct = 'gold' then
                    begin { gold }
                  image1.Picture.Graphic:= bmpsolarize(image1.picture.bitmap, 128);
                  image1.Picture.Graphic:= bmpflaxen(image1.picture.bitmap);
                  image1.Picture.Graphic:= bmpsepia(image1.picture.bitmap, 30);
                    end;  { gold }
               end; { i loop }
             if ButtonOriginal.Visible = False then ButtonOriginal.Show;
           finally
           Lines.Free;
           end; { try finally }
         end; { if open dialog }
    end;
This code is implemented in version 3.5 of my program. http://www.softpedia.com/get/Multimedia ... bber.shtml As I type this the new version has not been posted. For screen capture it works best if "show window contents while dragging" is enabled.

My unit of image effect functions is available on request. OR I can upload it as an attachment if there is sufficient demand for it.

Don
User avatar
mathgod
Junior Member
Junior Member
 
Posts: 35
Joined: December 15th, 2007, 4:36 pm
Location: Middle of New Mexico, USA

Re: reading text file into array of string

Postby Kambiz » January 12th, 2009, 5:03 am

Nice work, my compliments.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2429
Joined: March 7th, 2003, 7:10 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 3 guests

cron