随笔- 267  文章- 27  评论- 28  阅读- 10万 

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时,

 

 posted on   宝兰  阅读(3686)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示