如何读取已在使用的文本文件(Windows C ++) [英] How to read a text file that is already in use (Windows C++)

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

问题描述

我有一个应用程序,该应用程序使用 std :: ofstream 使用 std :: ofstream :: app 创建文本日志文件.此应用程序经常写入日志,有时仅相隔几毫秒.

I have an application that creates a text log file using an std::ofstream using std::ofstream::app to create it. This application is writing logs often, sometimes only milliseconds apart.

我想编写第二个应用程序,该应用程序可以读取并分析此日志文件,第一个应用程序仍在写入日志文件

I want to write a second application that reads in and analyses this log file whilst the first application is still writing to it

我有一些使用 ifstream 的工作代码,可以加载&处理一个独立的文本日志文件,但是当我尝试在当前正在通过该进程无法访问该文件,因为另一个进程正在使用该文件"的文本日志文件上使用它时,此代码失败.

I have some working code, using ifstream, that loads & processes a standalone text log file, but this code fails when I try to use it on the text log file that is currently being written to with "The process cannot access the file because it is being used by another process."

如何调整日志阅读器以允许我阅读当前正在写入的文本文件(Notepad ++可以在同一文件上执行该操作!)?

How can I adjust my log reader to allow me to read the text file currently being written to (which Notepad++ is able to do on the same file!)?

推荐答案

在Windows上,您可以将第三个参数传递给流构造函数/ open 以允许共享,例如

On Windows you can pass a third parameter to the stream constructors / open to allow sharing, e.g.

std::ofstream of("path", of.app, _SH_DENYNO);

默认情况下会传递

(int)ios_base :: _ Openprot ,这是设置为 _OPENPROT enum ,依次将其定义为 _SH_DENYNO ,所以它应该已经在不指定标志的情况下工作了,我不确定是不是为什么不这样做,您是否尝试过按照VS中的定义或调试代码?

(int)ios_base::_Openprot is passed by default which is an enum set to _OPENPROT, which is in turn defined as _SH_DENYNO so it should already work without specifying the flag, I'm not really sure why it doesn't in your case, have you tried following the definitions in VS or debugging the code?

参考:

http://msdn.microsoft.com/en-us/library/y1et11xw(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/kexhtshc(v=vs.110).aspx

这篇关于如何读取已在使用的文本文件(Windows C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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