想要将文件读取到 TStringList

发布于 2024-07-11 12:06:18 字数 523 浏览 6 评论 0原文

是的,我想将一个简单的日志文件读入 TStringList,这可以使用 LoadFromFile 轻松完成。 但问题是该文件可能已被另一个程序同时打开,因此可能会出现异常。 我尝试过使用:

FileMode := fmShareCompat;

但它不起作用。

我也尝试过使用:

fFilePath := fPathList[PathIndex] + '\' + FileData.Name;
AssignFile(vFile, fFilePath);
Reset(vFile, 1);  // Recordsize = 1

SetLength(vFileString, FileData.Size);
BlockRead(vFile, vFileString, FileData.Size);   
vCurrentFile.Text := vFileString;

它引发 EInOutError 并显示消息 I/O 错误 998。

有什么建议吗?

Yes I want to read a simple a logfile into a TStringList and that is easy done with LoadFromFile. But the problem is that the file can be already opened by another program at the same time so an exception may appear. I have tried to use:

FileMode := fmShareCompat;

But it won't work.

I have also tried to use:

fFilePath := fPathList[PathIndex] + '\' + FileData.Name;
AssignFile(vFile, fFilePath);
Reset(vFile, 1);  // Recordsize = 1

SetLength(vFileString, FileData.Size);
BlockRead(vFile, vFileString, FileData.Size);   
vCurrentFile.Text := vFileString;

It raise an EInOutError with message I/O error 998.

Any suggestion ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

输什么也不输骨气 2024-07-18 12:06:18

尝试 LoadFromStream 并执行以下操作:

fileStream := TFileStream.Create(aFileName, fmShareDenyNone);
myTStringList.LoadFromStream(fileStream);
fileStream.Free();

Try LoadFromStream and do something like:

fileStream := TFileStream.Create(aFileName, fmShareDenyNone);
myTStringList.LoadFromStream(fileStream);
fileStream.Free();
谁的新欢旧爱 2024-07-18 12:06:18

fmShareCompat 可能应该被标记为已弃用。 你想要fmShareDenyNone正如Drejc所说)

我相信 fmShareCompat 来自 16 DOS 天。 在 Windows 上,它的处理方式与 fmShareExclusive 相同。 当支持 Linux 时,它的处理方式与 fmShareDenyNone 相同。

fmShareCompat should probably be marked as deprecated. You want fmShareDenyNone (as Drejc said)

fmShareCompat comes from 16 DOS days I believe. On Windows, it is treated the same as fmShareExclusive. When Linux was supported it was treated the same as fmShareDenyNone.

尹雨沫 2024-07-18 12:06:18

另外,try.. except 和 try..finally 在这些时候都是好朋友。 将文件读取代码封装在这些类型的块中,并告诉用户出现的问题。

Also, try..except and try..finally are good friends at these times. Encapsulate your file reading code in these types of blocks and tell the user about the problem that arises.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文