如何编写正在读取的文件? [英] How to write a file that is being read?

查看:145
本文介绍了如何编写正在读取的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序缓慢地读取一个浮点数的TextFile。不时情况改变,文件必须重写。一些示例代码:

  procedure TAMI_Column_Selector.read_continuously(file_name:string); 
var infile:TextFile;
f:Double;
begin
AssignFile(infile,file_name);
重置(infile);
尝试
而不是EOF(infile)do
begin
Read(infile,f);
过程(f); //这可能需要一段时间,几秒甚至几分钟
end; // while
finally
CloseFile(infile);
end; // try..finally
end; // read_continuously //

如何编写一个可以阅读的文件?更具体地说:


  1. 我该如何编写一个可以阅读的文件?

  2. 如何在应用程序尝试读取正在写入
    的文件时防止崩溃

  3. 我的应用程序知道该文件已被重写?

我想我可以解决的第一个问题是将文件读入内存,并阅读(有没有一个TextFile可以写入和从内存中读取)?那么还有什么我可以测试一个文件已经写过。

任何人有一个(优雅的)解决这个问题?

在此先感谢。



在Windows 7上使用Delphi XE

解决方案要写入一个也可以阅读的文件,通常没有什么特别的作家需要做的。如果打开文件的其他人已经允许允许写入文件,则打算的作者可以打开文件进行写入,写入文件并关闭文件。如果其他人没有允许写入文件,那么原先的作者将不被允许打开文件,并且没有什么可以做的。



如何打开文件进行阅读,同时允许书写取决于正在使用的打开方法。使用 CreateFile dwDesiredAccess 参数通常是 GENERIC_READ ,而 dwShareMode 参数是 FILE_SHARE_READ或FILE_SHARE_WRITE 。如果你使用 TFileStream ,那么构造函数的 mode 参数应该是 fmOpenWrite或者fmShareDenyNone 。如果您使用 AssignFile Reset ,那么您需要设置 FileMode 全局变量,但不支持任何共享模式,所以你不能使用Pascal风格的I / O。



同时写入的文件本身不会造成崩溃。它在操作系统级别肯定不会造成问题。如果你的程序崩溃了,那是因为它没有被写入来预测读取失败。当您阅读某些内容时,请检查API结果以确认您读取的字节数与您要求的一样多。您也可以让读写应用程序相互沟通。您可能会使用同步对象来序列化对文件的访问,或者写入者可能会向读者发送一个信号来指示文件已经更改,并且之前的读取可能不再准确。这是由你来工作的细节。



如果读者要保存在内存中的文件的副本,那么它可能不需要打扰共享写访问。相反,它可以打开文件并仅共享读取权限,在内存中创建文件的副本,然后关闭文件。然后作者可以打开文件,而不必担心践踏读者的过程,因为没有什么可践踏的。它可以通知读者改变了一些东西,读者可以重新载入整个文件,或者只载入已经改变的部分。 (作者将不得不告诉读者哪部分已经改变了,没有别的方法可以让读者在没有阅读整个文件的情况下检测到它与存储器的不同之处)。

防止写入干扰读取的另一种方式是使用事务。尽管如此,事务性NTFS 正在被逐步淘汰。微软已经发布了替代品列表,所以你可以尝试找到符合您需求的东西。


My application reads a TextFile of floating point numbers slowly. From time to time circumstances change and the file has to be rewritten. Some example code:

procedure TAMI_Column_Selector.read_continuously (file_name: string);
var infile: TextFile;
    f: Double;
begin
   AssignFile (infile, file_name);
   Reset (infile);
   try
      while not EOF (infile) do
      begin
         Read (infile, f);
         process (f); // this may take quite some time, seconds or even minutes
      end; // while
   finally
      CloseFile (infile);
   end; // try..finally
end; // read_continuously //

How can I write a file that is open for reading? More specifically:

  1. how can I write a file that is open for reading?
  2. how to prevent a crash when the application tries to read a file that at that moment is being written
  3. how does my application know that the file has been rewritten?

I think I could solve the first to questions be reading the file into memory and read that (is there a TextFile that can be written to and read from memory)? Then still remains how I can test a file has been written over.

Anyone having an (elegant) solution to this problem?

Thanks in advance.

Using Delphi XE on windows 7

解决方案

To write to a file that's also open for reading, there's generally not anything special the writer needs to do. If everyone else who has the file open has allowed writing to the file, then the intended writer can open the file for writing, write to the file, and close it. If the others haven't allowed writing to the file, then the intended writer won't be allowed to open the file in the first place, and there's nothing it can do about it.

How to open a file for reading while also allowing writing depends on the opening method being used. With CreateFile, the dwDesiredAccess parameter is the usual GENERIC_READ, and the dwShareMode parameter is FILE_SHARE_READ or FILE_SHARE_WRITE. If you're using a TFileStream, then the mode parameter of the constructor should be fmOpenWrite or fmShareDenyNone. If you're using AssignFile and Reset, then you'd need to set the FileMode global variable, but that doesn't support any sharing modes, so you can't use Pascal-style I/O.

Reading a file that is at the same time being written does not inherently cause a crash. It certainly doesn't cause problems at the OS level. If your program crashes, it's because it wasn't written to anticipate reading failures. When you read something, check the API result to confirm that you read as many bytes as you requested. You can also have the reading and writing applications communicate with each other. You might use a synchronization object to serialize access to the file, or the writer might send the reader a signal to indicate that the file has changed, and that the previous read might not be accurate anymore. It's up to you to work the details.

If the reader is going to keep a copy of the file in memory, then it probably doesn't need to bother sharing write access. Instead, it can open the file and only share read access, make a copy of the file in memory, and the close the file. The writer can then open the file without any worries of trampling on the reader process because there's nothing to trample. It can notify the reader that something changed, and the reader can either reload the entire file or just load the part that changed. (The writer will have to tell the reader which part changed, though; there's no other way for the reader to detect that without reading the entire file and seeing how it differs from the memory copy.)

Another way to keep writes from interfering with reads is to use transactions. Transactional NTFS is being phased out, though. Microsoft has published a list of alternatives, so you can try to find something that matches your needs.

这篇关于如何编写正在读取的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆