[fpc-devel]Extend the procedure "Read/ReadLn" for text files?

Song Zhiwei songzw at mail.ustc.edu.cn
Mon May 24 10:54:34 CEST 2004


Extend the procedure "Read/ReadLn" for text files? And add new procedures
"ReadFromStr/ReadLnFromStr" and "WriteToStr/WriteLnToStr"?

procedure Read( [ var F: Text; ] V1 [, V2,...,Vn ] );
procedure ReadLn([ var F: Text; ] [ V1, V2, ...,Vn ]);

With a type string variable and followed by a regular expression:
  * Read a string by the regular expression and assign it to the string
      variable;
  * If no string is matched, try to skips any blanks, tabs, or end-of-line
      markers;
  * If no string is matched again, an I/O error occurs;

With a regular expression only:
  * Read a string by the regular expression and skip it;
  * If no string is matched, try to skips any blanks, tabs, or end-of-line
      markers;
  * If no string is matched again, an I/O error occurs;

Example

A text file 'record.txt':
John White : 90, 95.
Aileen: 80; 80.
 S Bob : 99 , 80 .

var
  F: Text;
  Scores: array of record
    Name: string;
    Math: Double;
    Music: Double;
   end;
begin
  Assign(F, 'record.txt');
  Reset(F);
  while not Eof(F) do begin
    SetLength(Scores, Length(Scores) + 1);
    with Scores[High(Scores)] do
      ReadLn(Name:'[a-zA-Z]+([ \t]+[a-zA-Z]+)?', ':', Math, '[,;]', Music, '.');
      // so easy, so convenient
  end;
  Writeln(Length(Scores), ' records read!');
end;

procedure ReadFromStr(const S: string; V1 [, V2, ...,Vn ]);
procedure ReadLnFromStr(const S: string [; V1, V2, ...,Vn ]);

Read values from a string like form a text file.

procedure WriteToStr(var S: string; V1 [, V2, ...,Vn ]);
procedure WriteLnToStrStr(var S: string [; V1, V2, ...,Vn ]);

Write values to a string like to a text file.





More information about the fpc-devel mailing list