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 ...
MrNetAsked:
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.

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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

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
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.