在读取文件的同时正在写另一个进程 [英] Reading a File while it is being written by another process

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

问题描述

我试图从一个缓冲区文件中读取二进制数据,这个缓冲区文件由不同的进程持续写入(我不能修改)。

  fileH = CreateFileA((LPCSTR)filename,
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);

它可以正确打开而不会出错。但是,当我从文件中读取数据时,似乎阻止了其他进程从写入文件到丢失数据。



缓冲区是循环的,这意味着文件大小是固定的,并且新数据不断地写在缓冲区中较旧的数据上。

$ p












$ b 我已经联系了软件公司,并告诉了他们这个错误,并在一天之内发布了一个修复的新版本。
对不起,这对每个人都不适用。

解决方案

正在打开文件。显然,它不打开文件进行独占访问,并保持打开。否则,您将无法读取它。



您所描述的行为表示写入过程打开文件进行独占访问,写入,然后关闭文件。如果是这样,那么你不能让你的程序打开文件并保持打开状态。如果你不能修改写入过程,那么你的选择是有限的,不是很有吸引力。最有可能的是,你必须让你的程序打开文件,读一个小块,关闭文件,然后再等一会再读。即使如此,也不能保证在写入过程试图写入时不会打开文件。我认为,你已经发现了。



你知道写作过程在无法打开文件时是否会丢失数据,或者只是缓冲数据,并在下次真正打开文件时写入数据?如果是这样的话,那么我一次一点一点地通过这个文件的建议可能会奏效。否则,你将失去数据。



没有开放模式,我知道这是相当于打开文件的阅读,但如果有人想要



另一种可能性是让您的程序在您想要读取的时候重命名文件,然后在您之后删除重命名的文件已经读过了。当然,这假定写作过程将在必要时创建一个新文件。即使如此,如果写入过程在重命名时尝试写入,那么可能会成为问题。我不认为这会是一个问题(就文件系统而言,重命名可能是原子),但这是你必须研究的。


I'm trying to read binary data from a buffer file which is continuously written to by a different process (that I cannot modify). I'm using the following code in order to open the file:

fileH = CreateFileA((LPCSTR)filename,
                    GENERIC_READ,
                    FILE_SHARE_READ | FILE_SHARE_WRITE,
                    NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL, NULL);

And it opens correctly with no error. However, when I read data from the file, it seems to block the other process from writing to the file since I loss data.

The buffer is circular, meaning that the file size is fixed, and new data is constantly written over older data in the buffer.

EDIT: Sometimes the most trivial solution works...

I've contacted the software company and told them about the bug, and within a day they posted a new version with a fix. Sorry this cannot work for everybody.

解决方案

It's hard to say what your options are without knowing how the writing process is opening the file. Obviously, it's not opening the file for exclusive access and keeping it open. Otherwise you wouldn't be able to read it at all.

The behavior you describe indicates that the writing process opens the file for exclusive access, writes to it, and then closes the file. If that's the case, then you can't have your program open the file and keep it open. That would cause the writing process to fail whenever it tried to write.

If you can't modify the writing process, then your options are limited and not very attractive. Most likely, you'll have to make your program open the file, read a small chunk, close the file, and then wait for a bit before reading again. Even then, there's no guarantee that you won't have the file open when the writing process tries to write. Which, I think, you have already discovered.

Do you know if the writing process loses the data when it can't open the file, or if it just buffers the data and writes it the next time it can actually open the file? If that's the case, then my suggestion of stepping through the file a little at a time could work. Otherwise, you're going to lose data.

There is no open mode that I know of that is the equivalent of "Open the file for reading, but if somebody wants exclusive access, then let them have it."

Another possibility would be to have your program rename the file whenever you want to read, and then delete the renamed file after you've read it. This assumes, of course, that the writing process will create a new file if necessary. Even then, there might be a problem if the writing process tries to write while you're renaming. I don't think that'll be a problem (the rename could be atomic as far as the file system is concerned), but it's something you'd have to research.

这篇关于在读取文件的同时正在写另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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