http://stackoverflow.com/questions/634587/delphi-why-do-i-sometimes-get-an-i-o-error-103-with-this-code
I don't see what is wrong with automatic retry. I don't see that you can do anything else.
If some other process is reading the file, then your Append/Rewrite will fail.
假如某个其他进程正在读取这个问题,这时你做Append Rewite 操作的话就会出错。
And since the file is a log, there is a good chance that something, such as a log viewer or a text editor, will be reading it at the instant you try to open it.
Try opening the file a few times with a delay in-between attempts before failing definitively.
在明确失败前,试试打开以一定的间隔打开这个文件几次。
You could use an exponential backoff if you wanted to be fancy.
你可以用一个变量控制。
try
ReWrite(MyFileHandler); // This sometimes fails
except
ReWrite(MyFileHandler); // When prior fails, this runs OK
end;
用户环境:
使用该软件已经快8年了,最近安装了360卫士和杀毒。
保存时提示该错误。
其他:
http://stackoverflow.com/questions/22026948/how-do-i-solve-i-o-error-103-in-delphi
http://stackoverflow.com/questions/16287983/why-do-i-get-i-o-error-32-even-though-the-file-isnt-open-in-any-other-program
http://mc-computing.com/languages/delphi/delphifileio.htm
测试:
procedure TForm1.Button1Click(Sender: TObject); var F,n: TextFile; const filestr = 's:\test.txt'; begin AssignFile(f, filestr); Append(f);// 不存在这个文件时,报File not found;存在时 无报错。 end
procedure TForm1.Button1Click(Sender: TObject); var F,n: TextFile; const filestr = 's:\test.txt'; begin // AssignFile(f, filestr);//无这行 报I/O error 102 Append(f); // Rewrite(f); // Writeln(f, 'abcs:\'); // CloseFile(f); end;
procedure TForm1.Button1Click(Sender: TObject); var F,n: TextFile; const filestr = 's:\test.txt'; begin AssignFile(f, filestr);//无这行 报I/O error 102 Append(f); Append(f);//第二次时点击按钮时 报 I/O error 32 资源管理器里删除该文件的话,提示 已经被其他程序打开 end;
procedure TForm1.Button1Click(Sender: TObject); var F,n: TextFile; const filestr = 's:\test.txt'; begin AssignFile(f, filestr); // Append(f);//无这行,报 I/O error 103 CloseFile(f); end;
procedure TForm1.Button1Click(Sender: TObject); var F,n: TextFile; const filestr = 's:\test.txt'; begin AssignFile(f, filestr); Append(f); Writeln(f, 'abcs:\'); CloseFile(n);//n变量 未关联文件时,报103 end;
可能的原因:
1、Append时,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现