problem in opening and reading csv file..HELP!!


[ Delphi Forum -- by DelphiLand ]

Posted by cherry on October 29, 2003:

hie ppl,
i have a problem here which i worked for few days....but still can't solve it. i don't know what's wrong with the code. it only read the 1st row and it stopped.below is my code. pls help......

C@l@ur (12:14 AM) :

procedure TForm1.Button1Click(Sender: TObject);
var
  DataNumber: integer;
  i, S, E: integer;
  TempArray: Array of String;
begin
  if OpenDialog1.Execute then
  begin
    GetLocaleFormatSettings(LCID_INSTALLED, DateFormat);
    DateFormat.shortDateFormat := 'DD/MM/YYYY';
    DateFormat.DateSeparator:= '/';
    AssignFile(csvFile, OpenDialog1.FileName);
    SetLength(TempArray, 0);
    if FileExists(OpenDialog1.FileName) then
    begin
      Reset(csvFile);
      //readn the headingsn first and discard
      Readln(CSVFile, CSVFileLine);
      //clear the myDataArray
      SetLength(TempArray, 0);
      //cycle through the csv file extracting the data into the record
      //convert the csv into the record and store in the array
      while not EOF(CSVFile) do
      begin
        Readln(CSVFile, CSVFileLine);
        s := 1;
        for i := 1 to length(CSVFileLine)+1  do
        begin
          if (CSVFileLine[i] = ',') or (i = length(CSVFileLine) + 1) then
          begin
            SetLength(tempArray, Length(tempArray) + 1);
            e := i + s;
            tempArray[Length(tempArray) - 1] := Copy(csvFileLine, s, e);
            s := i + 1;
          end;
        end;
     
        SetLength(Bookarray,Length(tempArray)+1);
        Bookarray[current].ISBN := tempArray[0];
        Bookarray[current].Tittle:= Temparray[1];
        Bookarray[current].Author := Temparray[2];
        Bookarray[current].Publisher:= Temparray[3];
        if TempArray[4]  ' ' then
        begin
          TempDate:= StrToDate(TempArray[4], DateFormat);
          DecodeDate(TempDate, Year, Month, Day);
          Bookarray[current].PublicationYear:= Year;
        end;
      end;
      begin
        edtisbn.text := Bookarray[current].ISBN;
        edtauthor.text := Bookarray[current].Author;
        edttittle.Text := Bookarray[current].Tittle;
        edtpublicationyear.text := inttostr(Bookarray[current].publicationyear);
        edtpublisher.Text := Bookarray[current].Publisher;
      end;
    end
    else
    begin
      SetLength(BookArray, 0);
      Rewrite(BookFile);
    end;
    //ToggleButtons;
end;

Related Articles and Replies:


[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]