[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

How To read from CommaText File

I want to read from a text file with comma seperated lines .. like:

Sunday,item1,item2,item3
Monday,item1,item2,item3
Tuesday,item1,item2,item3
Wednesday,item1,item2,item3
Thursday,item1,item2,item3
Friday,item1,item2,item3
Saterday,item1,item2,item3

so on ...

keep on mind that i want know in my code that item1 at line 1 is for Monday and like this ...
0
MrNet
Asked:
MrNet
1 Solution
 
haehnenCommented:
Hi,
why don't you use IniFiles?
ie:

uses
  IniFiles;

var Ini: TIniFile;
begin
 Ini := TIniFile.Create('test.ini');
 Label1.Caption := Ini.ReadString('Sunday', 'item1', '');
 Label2.Caption := Ini.ReadString('Sunday', 'item2', '');

and so on .. :-)

bye
0
 
kretzschmarCommented:
hi mrnet,

for reading into a stringgrid you can do something like this

procedure TForm1.Button1Click(Sender: TObject);
var
  F : TextFile;
  S : String;
begin
  assignFile(f,'c:\Text.Txt');  //or whatever filename
  stringgrid1.RowCount := 0;
  stringgrid1.ColCount := 5;
  try
    Reset(F);
    try
      while not eof(f) do
      begin
        readln(f,s);
        stringgrid1.RowCount := stringgrid1.RowCount + 1;
        StringGrid1.Rows[StringGrid1.RowCount-1].CommaText := s;
      end;
      StringGrid1.FixedRows := 1;
    finally
      closefile(f);
    end;
  except
    //an error
  end;
end;

but you've never said, where you need it, well, with this you can now access the values with the grid-coordinates using the stringgrid.cells method

meikl
0
 
simonetCommented:
Another option is to treat the CSV file as a Table, by using a schema file.

Let me know if you need more info on that.

haehnen,

The file MrNet posted as example CANNOT be read as an Ini file. Ini files have the following format:

Sunday=item1,item2,item3

BEsides, you must know beforehand all the "values" on the file.

By using the Table-SCH approach, handling the file is just like handling a Table or query objects.

Alex
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
edeyCommented:
actually, if the file is given an ini section header, [some section] then you can use the readsectionvalues method.  


GL
Mike
0
 
interCommented:
Hi,
May be the easiest way is by using *magical*  TStringList as usuall.

procedure ProcessCommaDelimited(FileName : string);
var
  FLines : TStrings;
  FRow : TStrings;
begin
  FLines := TStringList.Create;
  try
    FLines.LoadFromFile(FileName);
    FRow := Tstringlist.create;
    try
       for I := 0 to FLines.Count-1 do
       begin
           //stringlist could auto parse comma delimited strings
           // after the following statement you have the first item of I th
           // line of file in FRow[0], 1st in FRow[1] etc.. so do what you
           // want with them
           FRow.CommaText := FLines[I];
       end;
    finally
      FRow.Free;
    end;
  finally
    FLines.Free;
  end;
end;

regards, igor
0
 
MrNetAuthor Commented:
Thanks Inter,
Thanks for every body.

That's write .. this MAGIC is from TStringList is:

var
  MyStringList : TStringList;
begin
  MyStringList := TStringList.Create;
  MyStringList.CommaText := 'MrNet,Inter,Edey,Simonet,kretzschmar,haehnen' // :)

{ after that ..
  MyStringLis[1] = 'MrNet'
  MyStringLis[2] = 'Inter'
  .....
  MyStringLis[6] = 'haehnen'}

end;
0
 
MrNetAuthor Commented:
Don't Forget to write:
  MyStringList.Free; // at the end
0
 
MrNetAuthor Commented:
Inter,

Please Mark your Comment as an Answer

Thanx
0
 
interCommented:
Happy to see it works,

procedure ProcessCommaDelimited(FileName : string);
var
  FLines : TStrings;
  FRow : TStrings;
begin
  FLines := TStringList.Create;
  try
    FLines.LoadFromFile(FileName);
    FRow := Tstringlist.create;
    try
       for I := 0 to FLines.Count-1 do
       begin
       //stringlist could auto parse comma delimited strings
       // after the following statement you have the first item of Ith
       // line of file in FRow[0], 1st in FRow[1] etc.. so do what you
       // want with them
         FRow.CommaText := FLines[I];
       end;
    finally
      FRow.Free;
    end;
  finally
    FLines.Free;
  end;
end;

regards, igor
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now