e part of a full path name } uses SysUtils; function NamePart(FileName: PChar): PChar; var P: PChar; begin P := StrRScan(FileName, ''''\''''); if P = nil then begin P := StrRScan(FileName, '''':''''); if P = nil then P := FileName; end; NamePart := P; end; var S : string; begin S := StrPas(NamePart(''''C:\Test.fil'''')); Canvas.TextOut(10, 10, S); end; const S: PChar = ''''MyFile.zzz''''; var R: PChar; begin R:= StrRScan( S, ''''.''''); { R := ''''.zzz'''' } MessageDlg( R, mtInformation, [mbOk], 0); end; ----------------------------------------------------------------------------- StrScan 子字元在母字串中的位置. ----------------------------------------------------------------------------- Unit SysUtils 函数原型 function StrScan(Str: PChar; Chr: Char): PChar; 范例 uses SysUtils; function HasWildcards(FileName: PChar): Boolean; { Return true if file name has wildcards in it } begin HasWildcards := (StrScan(FileName, ''''*'''') <> nil) or (StrScan(FileName, ''''?'''') <> nil); end; const P: PChar = ''''C:\Test.* ''''; begin if HasWildcards(P) then Canvas.TextOut(20, 20, ''''The string has wildcards'''') else Canvas.TextOut(20, 20, ''''The string doesn''''t have wildcards'''') end; const S: PChar = ''''http://www.atrussk.ru''''; var R: PChar; begin R:= StrScan( S, ''''w''''); { R := ''''www.atrussk.ru'''' } MessageDlg( R, mtInformation, [mbOk], 0); end; ----------------------------------------------------------------------------- StrUpper 将字串全部转为大写字母. ----------------------------------------------------------------------------- Unit SysUtils 函数原型 function StrUpper(Str: PChar): PChar; 范例 uses SysUtils; const S: PChar = ''''A fUnNy StRiNg'''' begin Canvas.TextOut(5, 10, StrPas(StrLower(S)) + '''' '''' + StrPas(StrUpper(S))); end; ========================================= Text-file routines Text-file常式 ========================================= Append 开起一个可供Append的档案. ----------------------------------------------------------------------------- Unit System 函数原型 procedure Append(var f: Text); 范例 var F: TextFile; begin if OpenDialog1.Execute then { Bring up open file dialog } begin AssignFile(F, OpenDialog1.FileName); { Open file selected in dialog } Append(F); { Add more text onto end } Writeln(F, ''''appended text''''); CloseFile(F); { Close file, save changes } end; end; Example var
f: TextFile; begin if OpenDialog1.Execute then begin { open a text file } AssignFile(f, OpenDialog1.FileName); Append(f); Writeln(f, ''''I am appending some stuff to the end of the file.''''); { insert code here that would require a Flush before closing the file } Flush(f); { ensures that the text was actually written to file } CloseFile(f); end; end; ## Append, Flush Example ----------------------------------------------------------------------------- Eoln 测试档案是否结束.(For text file.) ----------------------------------------------------------------------------- Unit System 函数原型 function Eoln [(var F: Text) ]: Boolean; Flush 将Buffer中的资料存入磁碟. (For text file) Unit System 函数原型 procedure Flush(var F: Text); 范例 var f: TextFile; begin if OpenDialog1.Execute then begin { open a text file } AssignFile(f, OpenDialog1.FileName); Append(f); Writeln(f, ''''I am appending some stuff to the end of the file.''''); Flush(f); { ensures that the text was actually written to file } { insert code here that would require a Flush before closing the file } CloseFile(f); end; end; Example begin { Tells program to wait for keyboard input } WriteLn(Eoln); end; ----------------------------------------------------------------------------- Read 读档. ----------------------------------------------------------------------------- Unit System 函数原型 procedure Read(F , V1 [, V2,...,Vn ] ); procedure Read( [ var F: Text; ] V1 [, V2,...,Vn ] ); 范例 uses Dialogs; var F1, F2: TextFile; Ch: Char; begin if OpenDialog1.Execute then begin AssignFile(F1, OpenDialog1.Filename); Reset(F1); if SaveDialog1.Execute then begin AssignFile(F2, OpenDialog1.Filename); Rewrite(F2); While not Eof(F1) do begin Read(F1, Ch); Write(F2, Ch); end; CloseFile(F2); end; CloseFile(F1); end; end. ----------------------------------------------------------------------------- Readln 读档. ----------------------------------------------------------------------------- Unit System 函数原型 procedure Readln([ var F: Text; ] V1 [, V2, ...,Vn ]); 范例 var s : string; begin Write(''''Enter a line