请高手检查我的代码哪出问题,不时报异常,谢谢(200)

W

wrxljf

Unregistered / Unconfirmed
GUEST, unregistred user!
nldo:=0; nlst:=0; for nltmpst:=1 to nllen do begin if cfor[nltmpst]='/' then nldo:=nltmpst else continue; end; if nlformat=0 then begin nlformat:=1; //if fileExists(FileName) then DeleteFile(FileName); {看文件是否存在,在就刪除} //AssignFile(F, FileName); {将文件名与变量 F 关联} // //Writeln(F, '将您要写入的文本写入到一个 .txt 文件'); //Closefile(F); {关闭文件 F} //cLt:='C:/cL_fmt_temp.txt'; AssignFile(cLformatfile, clt1); {将文件名与变量 F 关联} if fileExists(clt1) then DeleteFile(Pchar(clt1)); //ReWrite(clformatfile); {创建一个新的文件并命名为 C:/cL_fmt_temp.txt} CloseFile(clformatfile); Append(clformatfile); Writeln(clformatfile,copy(string(cfor),1,nldo+1)); CloseFile(clformatfile); end else begin try Append(clformatfile); Writeln(clformatfile,copy(string(cfor),1,nldo+1)); CloseFile(clformatfile); except nlformat:=0; CloseFile(clformatfile); if fileExists(clt1) then DeleteFile(Pchar(clt1)); ShowMessage('写格式文件出错,请重试'); end; end;说明:clt1是一个字符串,声明部分如下:function OmrS(cfor:pchar):integer;stdcall;var ks,nllen,nlre,nlfr,nldo,nlst:integer; nltmpst:integer; cLformatfile: Textfile; clt,clt1,clpath:string; //cst:string;begin cLt:='C:/cLfmt.txt'; clt1:=clt;我的操作是:对一个文件用一次重建一次,每次都向其中写入几十条不到一百条数据,执行的话也不是每次都失败,但执行次数不定就会异常,请高手们指教,我哪错了.感激不尽.....
 
//ReWrite(clformatfile); {创建一个新的文件并命名为 C:/cL_fmt_temp.txt}这行是我贴上去前注销的,就是不注销,也一样不行.如果异常,跳过去后再执行,就把前面的操作全部清空了.好象nlformat标识也不起作用了.令人郁闷.
 
检查一下你希望删除的那个文件是否有时候处于只读状态,如果发生只读,Delete函数会报异常。对于文件操作,可以在适当位置增加 IOResult() 测试函数,看看是什么操作导致的问题。
 
To 小雨哥,你可能没有看仔细,文件不可能是只读状态,因为不是每次都异常,是在某个不确定的时候异常.对IO方面操作不是太好,请多指教.
 
跟一下就知道问题出在什么地方
 
试试下面的流程:procedure LogFile; // 写日志文件var F: TextFile;begin try if not FileExists(LogFn) then // 如果没有文件则新建 begin AssignFile(F, LogFn); Rewrite(F); CloseFile(F); end; AssignFile(F, LogFn); Append(F); Writeln(F, DateTimeToStr(Now) + ' ' + ALog); CloseFile(F); except Exit; end;end;LogFn中包含路径,建议你的文件名都要包含路径,在检查检查是否有什么全局变量没有释放等问题。
 
我跟了一下,是在关掉文件时出错.为什么会错呢?
 
你把错误信息贴出来。另外可以用GetTempFileName生成临时文件名,然后使用,最后再把自己用过的临时文件清除。
 
问题已基本上解决.采用流方式就没有这些错了.散分....
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部