请教如何让两个程序同时读取一个文件,而不产生异常呢?

weixin_38301476 2017-06-05 05:39:12
请教各位大神一个C++程序开发的问题: 程序A没隔一段时间会去更新一个log.txt文件,程序B每秒都去读取同一个log.txt文件,如何才能确保不产生冲突而导致程序崩溃呢?
PS: 程序A是别人写好的,我没法改,而且它更新的频率也不确定,并不是每秒一次。我只能改写程序B,且可以控制读取的频率,但是不能频率过低(比如1s一次或者2s一次,再长久不行了)。
...全文
1252 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
谁学逆向工程 2017-06-10
  • 打赏
  • 举报
回复
引用 14 楼 zhao4zhong1 的回复:
英语也是一门计算机语言的说。
你这翻译的啥
CGabriel 2017-06-09
  • 打赏
  • 举报
回复
提供一个简单快捷方便暴力的法子: 捕捉异常或者留意返回值,如果出现不成功则重试。
赵4老师 2017-06-09
  • 打赏
  • 举报
回复
英语也是一门计算机语言的说。
谁学逆向工程 2017-06-09
  • 打赏
  • 举报
回复
引用 10 楼 zhao4zhong1 的回复:
http://fanyi.baidu.com/#en/zh/Dumping%20File%20Sectors%20Directly%20from%20Disk%20using%20Logical%20Offsets
这东西翻译不了专业属于,您老给解释一下
赵4老师 2017-06-07
  • 打赏
  • 举报
回复
引用 9 楼 xiaoyuanyuan2009 的回复:
[quote=引用 6 楼 zhao4zhong1 的回复:] Dumping File Sectors Directly][/url]
这咋翻译,立即转储扇形文件?意思是复制一个出来自己打开?[/quote] http://fanyi.baidu.com/#en/zh/Dumping%20File%20Sectors%20Directly%20from%20Disk%20using%20Logical%20Offsets
weixin_38301476 2017-06-07
  • 打赏
  • 举报
回复
所以我需要用参数 _SH_DENYRW来实现 Denies read and write access to file, 这样程序B就可以顺利读文件,而不用考虑A是否在使用文件了吗? 顺便说一句:程序A是修改文件,不是追加。
赵4老师 2017-06-06
  • 打赏
  • 举报
回复
引用 5 楼 xiaoyuanyuan2009 的回复:
我认为,只能让A以不独占的方式打开
话不能说的那么绝对! FDump - Dumping File Sectors Directly from Disk using Logical Offsets http://www.codeproject.com/Articles/32169/FDump-Dumping-File-Sectors-Directly-from-Disk-usin
谁学逆向工程 2017-06-06
  • 打赏
  • 举报
回复
我认为,只能让A以不独占的方式打开
赵4老师 2017-06-06
  • 打赏
  • 举报
回复
乍看起来c++的cin、cout、ifstream、ofstream、istringstream、ostringstream在输入、输出上比c的scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf简单,不用格式控制符! 但是不用格式控制符,输入输出恰好是你期望的格式的时候好说;等到输入输出不是你期望的格式的时候,你就会觉得还是用格式控制符更方便、更靠谱。 摒弃cin、cout、ifstream、ofstream、istringstream、ostringstream! 使用scanf、printf、fscanf、fprintf、fread、fwrite、sscanf、sprintf。
谁学逆向工程 2017-06-06
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
Dumping File Sectors Directly][/url]
这咋翻译,立即转储扇形文件?意思是复制一个出来自己打开?
weixin_38301476 2017-06-06
  • 打赏
  • 举报
回复
[quote=引用 1 楼 zhao4zhong1 的回复:] _fsopen, _wfsopen Open a stream with file sharing. FILE *_fsopen( const char *filename, const char *mode, int shflag ); FILE *_wfsopen( const wchar_t *filename, const wchar_t *mode, int shflag ); 谢谢大神指点! 请问用 _fsopen 打开后如何读入内容呢? 这个函数可以ifstream同时使用吗? 我debug了一下会报内存错误。可以帮我看一下吗? ReadOrderFile(string filename) { FILE *stream; ifstream infile; ostringstream buf; char order; if ((stream = _fsopen(filename.c_str(), "r", _SH_DENYNO)) != NULL) { infile.get(order); buf.put(order); } infile.close(); return buf.str(); }
yangyunzhao 2017-06-06
  • 打赏
  • 举报
回复
看文件名,应该是日志吧,这类文件的特点是只追加,不会修改和删除。 同时读写应该问题不大
LubinLew 2017-06-06
  • 打赏
  • 举报
回复
我的理解是: 如果A只是追加的话,那么B正常读就可以了,不过要做数据完整性检测; 如果A是修改的话,那么只能读写互斥了吧,要不然容易读取到垃圾数据,最好的方法是A更新一次写一个文件,B读取一次删除这个文件. Note: 如何只是分析日志,那么用锁挂起一个进程,是不太合理的方式,如果是重要数据,要考虑一下使用进程间通信等方式传递
ztenv 版主 2017-06-05
  • 打赏
  • 举报
回复
共享方式打开,如果他的程序不是共享方式的话,可以改他的汇编代码。
赵4老师 2017-06-05
  • 打赏
  • 举报
回复
A将请求数据写到文件a.txt,写完后改名为aa.txt B发现aa.txt存在时,读取其内容,调用相应功能,将结果写到文件b.txt,写完后删除aa.txt,改名为bb.txt A发现bb.txt存在时,读取其内容,读完后删除bb.txt 或者: _fsopen, _wfsopen Open a stream with file sharing. FILE *_fsopen( const char *filename, const char *mode, int shflag ); FILE *_wfsopen( const wchar_t *filename, const wchar_t *mode, int shflag ); Function Required Header Optional Headers Compatibility _fsopen <stdio.h> <share.h>1 Win 95, Win NT _wfsopen <stdio.h> or <wchar.h> <share.h>1 Win NT 1 For manifest constant for shflag parameter. For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value Each of these functions returns a pointer to the stream. A NULL pointer value indicates an error. Parameters filename Name of file to open mode Type of access permitted shflag Type of sharing allowed Remarks The _fsopen function opens the file specified by filename as a stream and prepares the file for subsequent shared reading or writing, as defined by the mode and shflag arguments. _wfsopen is a wide-character version of _fsopen; the filename and mode arguments to _wfsopen are wide-character strings. _wfsopen and _fsopen behave identically otherwise. Generic-Text Routine Mappings TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined _tfsopen _fsopen _fsopen _wfsopen The character string mode specifies the type of access requested for the file, as follows: "r" Opens for reading. If the file does not exist or cannot be found, the _fsopen call fails. "w" Opens an empty file for writing. If the given file exists, its contents are destroyed. "a" Opens for writing at the end of the file (appending); creates the file first if it does not exist. "r+" Opens for both reading and writing. (The file must exist.) "w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed. "a+" Opens for reading and appending; creates the file first if it does not exist. Use the "w" and "w+" types with care, as they can destroy existing files. When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus existing data cannot be overwritten. When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for “update”). However, when switching between reading and writing, there must be an intervening fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired. In addition to the above values, one of the following characters can be included in mode to specify the translation mode for new lines: t Opens a file in text (translated) mode. In this mode, carriage return–linefeed (CR-LF) combinations are translated into single linefeeds (LF) on input and LF characters are translated to CR-LF combinations on output. Also, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading or reading/writing, _fsopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z may cause fseek to behave improperly near the end of the file. b Opens a file in binary (untranslated) mode; the above translations are suppressed. If t or b is not given in mode, the translation mode is defined by the default-mode variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL. For a discussion of text and binary modes, see Text and Binary Mode File I/O. The argument shflag is a constant expression consisting of one of the following manifest constants, defined in SHARE.H: _SH_COMPAT Sets Compatibility mode for 16-bit applications _SH_DENYNO Permits read and write access _SH_DENYRD Denies read access to file _SH_DENYRW Denies read and write access to file _SH_DENYWR Denies write access to file Example /* FSOPEN.C: */ #include <stdio.h> #include <stdlib.h> #include <share.h> void main( void ) { FILE *stream; /* Open output file for writing. Using _fsopen allows us to * ensure that no one else writes to the file while we are * writing to it. */ if( (stream = _fsopen( "outfile", "wt", _SH_DENYWR )) != NULL ) { fprintf( stream, "No one else in the network can write " "to this file until we are done.\n" ); fclose( stream ); } /* Now others can write to the file while we read it. */ system( "type outfile" ); } Output No one else in the network can write to this file until we are done. Stream I/O Routines See Also fclose, _fdopen, ferror, _fileno, fopen, freopen, _open, _setmode, _sopen

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧