文档介绍:Delphi读取文件和写入文件总结
Delphi读取文件和写入文件总结
---
读取文件:
1,关联文件:Assign,'c:\'); 2,打开文件:Reset(pMyFile);
3,读取一行:Readln(pMy);
4,关闭文件:Close);
示例:
procedure (Sender: TObject);
var
pMy;
pStr : string;
begin
if then begin
Assign);
Reset(pMyFile);
while not Eof(pMyFile) do begin
Readln(pMy);
//fn_DelStock(pStr); //使用读取的字符串相关语句
next;
end;
Close);
end;
end;
+++
写入文件:
1,关联文件:Assign,'c:\'); 2,打开文件:ReWrite(pMyFile); //如果文件不存在,用ReWrite打开
Append(pMyFile); //如果文件已经存在,追加内
容,用Append打开
3,写入一行:WriteLn(pMy); 4,关闭文件:Close);
示例:
procedure (pMsg: string); var
pFObJect,pFPath,pFName: string; pMyFile: textFile;
pStr: string;
begin
pFPath:='d:';
pFName:='StockDel_'+formatDateTime('yyyymmddhhmmss',now);
pFObject:=pFPath + '\' + pFName + '.csv';
try
Assign);
if (pFObject)=false then
ReWrite(pMyFile)
else
Append(pMyFile);
pStr:=pMsg;
WriteLn(pMy);
finally
Close);
end;
end;
+++
公用写日志文件过程
//==ini文件设置:
'日志选项和文件 当Log_Flag=N时不记录,否则均记录
Log_Flag=1
Log_Path\\\c\Prd_220_ //==声明全局变量
x_pLogFile: string; //日志文件名称
x_pLogFlag: string; //是否记录日志,N:不写日志
x_pFindLogFile: boolean; //记录日志文件是否存在,避免每次写
日志时都要判断。
//==过程声明
procedure cpWriteLog(pFObject:string; pTxt:string; pMode:byte);
//==初始化全局变量
procedure (Sender: TObject); begin
x_pLogFile:=
cfReadIni,'Log_Path','c:\');
x_pLogFlag:= cfReadIni,'Log_Flag','N'); end;
//==写日志过程
procedure cpWriteLog(pFObject:string; pTxt:string; pMode:byte);
var
pMyFile: textFile;
begin
if x_pLogFlag='N' then exit;
try
Assign);
if pMode=1 then pTxt:= DateTimeToStr(Now)+' '+pTxt;
if x_pFindLog then
append(pMyFile)
else begin
if (pFObject) then
append(pMyFile)
else
reWrite(pMyFile);
x_pFindLog;
end;
WriteLn(pMy);
finally
Close);
end;
end;
//==调用过程
x_pMsg:='导出程序['+X_cProgID+']打开,';
cpWriteLog(x_pLog);
Delphi文件操作