2008. február 15., péntek

How to open a CSV file and assign each of its lines to a variable


Problem/Question/Abstract:

What is the easiest way to import a comma delimited text file and assign the different elements of a line of data in that file to variables?

Answer:

function GetNextField(var Line: string; Sep: Char = '|'): string;
{Extracts the first field from Line, delimited by Sep, using the
pipe character as the default delimeter}
var
  SepPos: Integer;
begin
  {Finds the position of the first occurrence of Sep in Line}
  SepPos := Pos(Sep, Line);
  {If found...}
  if SepPos > 0 then
  begin
    {There are fields; copy the first to Result}
    Result := Copy(Line, 1, SepPos - 1);
    {Delete first field from Line, including the delimeter}
    Delete(Line, 1, SepPos);
  end
  else
  begin
    {No more fields; copy entire Line to Result}
    Result := Line;
    {Return a null Line}
    Line := '';
  end;
end;

This function can be used with TextFiles, FileStreams, MemoryStreams, StringLists, arrays of strings, etc. I will give you a very basic example of how I write and then read back text files. Here is a writer:

procedure WriteToFile;
var
  Line: string;
  TempFile: TextFile;
begin
  {Initialize the file}
  AssignFile(TempFile, 'Some\Path\Here');
  {Open the file for output}
  Rewrite(TempFile);
  try
    {Scan source table until EOF}
    MyTable.First;
    while not MyTable.EOF do
    begin
      {Build the line}
      Line := MyTableAINTEGERFIELD.AsString + '|';
      Line := Line + MyTableAFLOATFIELD.AsString + '|';
      Line := Line + MyTableASTRINGFIELD.AsString + '|';
      {Write the line}
      Writeln(TempFile, Line);
      {Move to next record}
      MyTable.Next;
    end;
  finally
    {Close the file}
    CloseFile(TempFile);
  end;
end;

And here is a reader:

procedure ReadFromFile;
var
  AInteger: Integer;
  AFloat: Extended;
  AString, Line: string;
  TempFile: TextFile;
begin
  {Initialize the file}
  AssignFile(TempFile, 'Some\Path\Here');
  {Open the file for input}
  Reset(TempFile);
  try
    {Read lines until EOF}
    while not Eof(TempFile) do
    begin
      {Read a line}
      Readln(TempFile, Line);
      {Assign fields to variables}
      AInteger := StrToInt(GetNextField(Line));
      AFloat := StrToFloat(GetNextField(Line));
      AString := GetNextField(Line);
    end;
  finally
    {Close the file}
    CloseFile(ArqTexto);
  end;
end;

These are only basic examples. You must fine tune the error handling to your needs. And, of course, the examples assume your table is allready open and you know in advance how many fields there are in a line.

Nincsenek megjegyzések:

Megjegyzés küldése