我可以让多个程序从同一文件在同一时间阅读? [英] Can I allow multiple programs to read from the same file at the same time?

查看:240
本文介绍了我可以让多个程序从同一文件在同一时间阅读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有读取一组数据文件并执行一些模型计算的应用程序。该程序并不需要修改数据文件本身,所以我目前正在与只读标志打开它们,如下所示:

I have an application that reads a set of data files and performs some model computations. The program does not need to modify the data files themselves, so I'm currently opening them with the read-only flag as shown below:

FILE* file;
if(_wfopen_s(&file, fname.c_str(), L"r") == 0)
...

我想有在同一时间运行,采用相同的一组数据,但对数据执行不同的计算我的程序的多个实例。我的节目都不需要修改的数据文件。由于数据文件非常大,我不能使数据的单独副本,每个程序使用。

I would like to have several instances of my program running at the same time, using the same set of data, but performing different computations on the data. None of my programs need to modify the data files. As the data files are very large, I can't make separate copies of the data to use with each program.

我以为,因为我打开与只读权限的文件,这两个程序可以从相同的文件在同一时间阅读。相反,我相处的线各种错误的文件无法打开,因为它正被另一个进程使用。

I assumed that because I'm opening the files with read-only permissions, two programs could be reading from the same file at the same time. Instead, I get various errors along the lines of "the file could not be open because it is being used by another process".

由于我的开发环境是Windows 7,这个问题表明,它可能是能够读取共享的问题。然而,所有在该线程的答案依靠的CreateFile ,而我处理那些与stdio.h中写传统code。

As my development environment is Windows 7, this question suggests it might be a matter of enabling read sharing. However, all of the answers in that thread rely on CreateFile, whereas I'm dealing with legacy code that was written with stdio.h.

有没有一种方法可以让我有几个程序同时使用的fopen类的函数从文件中读取?

Is there a way I can have several programs concurrently read from a file using the fopen class of functions?

推荐答案

如果你可以改变的fopen 常规,然后尝试更换的fopen 使用 _fsopen ,共享读/写。 _fsopen 是mscrt特定的。

If you can change the fopen routine then try to replace fopen calls with _fsopen, for shared reading/writing. _fsopen is mscrt-specific.

如果您的可以的使用的CreateFile ,又不想重新写遗留下来的所有code读/写,你也可以尝试一个相关联 FILE * 用WINAPI文件句柄。使用 _open_osfhandle 来从一个文件的文件描述符由的CreateFile 返回的句柄,然后使用的 _fdopen 获得 FILE * 从该文件描述符。

If you can use CreateFile, and don't want to re-write all the legacy code for read/write, you can also try associating a FILE * with a winapi file handle. Use _open_osfhandle to get a file descriptor from a file handle returned by CreateFile, then use _fdopen to get a FILE * from that file descriptor.

这篇关于我可以让多个程序从同一文件在同一时间阅读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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