在线程中访问文件

切变以 2015-07-21 11:57:16
有时在线程中打开一个文本文件,刚到FOPEN那就不动了,导致线程一直停在那儿。但有时什么地方也没改自己又能正常打开了,用的VC6编译器,XP系统。在主线程中打开没问题,文件也没有被其它地方打开。
...全文
156 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-07-27
  • 打赏
  • 举报
回复
英语也是一门计算机语言的说。
切变以 2015-07-25
  • 打赏
  • 举报
回复
引用 11 楼 赵4老师的回复:
对本ID回帖视而不见的坛友会浪费一大截生命!
不是视而不见,主要是看不懂呀,英语太菜。麻烦能不能把重点的地方给指点一下。
切变以 2015-07-24
  • 打赏
  • 举报
回复
引用 9 楼 qq_30015657的回复:
[quote=引用 7 楼 qq_30015657 的回复:] [quote=引用 6 楼 菜的不一般的回复:]在while处设置一个断点,然后单步调试下看看能不能往下走
就是打的断点,到fopen那再走一步就不动了。[/quote] 下一行就进不了断点。[/quote] 顶一下,高手帮忙呀
赵4老师 2015-07-24
  • 打赏
  • 举报
回复
对本ID回帖视而不见的坛友会浪费一大截生命!
zgl7903 2015-07-22
  • 打赏
  • 举报
回复
是打开固定文件吗? 是绝对路径吗? 相对路径可能导致系统会搜索PATH环境变量指定的路径 有检测返回值吗? 是不是后续的访问有抛出的异常而导致线程阻塞 TRACE或LOG看看程序的执行流程和使用的时间,以追踪最终原因
切变以 2015-07-22
  • 打赏
  • 举报
回复
引用 3 楼 csdn1223537114的回复:
代码发给我看看,说不定我给你解决了呢?
dword winapi thread1(lpvoid lpparameter) {while(1) {FILE *f; f=fopen("c:\\A.txt","a+t"); 到这儿就停了
Magic丶旭 2015-07-22
  • 打赏
  • 举报
回复
代码发给我看看,说不定我给你解决了呢?
切变以 2015-07-22
  • 打赏
  • 举报
回复
引用 7 楼 qq_30015657 的回复:
[quote=引用 6 楼 菜的不一般的回复:]在while处设置一个断点,然后单步调试下看看能不能往下走
就是打的断点,到fopen那再走一步就不动了。[/quote] 下一行就进不了断点。
切变以 2015-07-22
  • 打赏
  • 举报
回复
引用 1 楼 zgl7903的回复:
是打开固定文件吗? 是绝对路径吗? 相对路径可能导致系统会搜索PATH环境变量指定的路径 有检测返回值吗? 是不是后续的访问有抛出的异常而导致线程阻塞 TRACE或LOG看看程序的执行流程和使用的时间,以追踪最终原因
是固定文件,也是绝对路径。没有返回值,就停那不动了。
Magic丶旭 2015-07-22
  • 打赏
  • 举报
回复
引用 3 楼 csdn1223537114 的回复:
代码发给我看看,说不定我给你解决了呢?
理论上只要线程正在运行,代码是不会停的,你在这一句下一行代码设一个断点调试一下就知道了啊
切变以 2015-07-22
  • 打赏
  • 举报
回复
引用 6 楼 菜的不一般的回复:
在while处设置一个断点,然后单步调试下看看能不能往下走
就是打的断点,到fopen那再走一步就不动了。
lx624909677 2015-07-22
  • 打赏
  • 举报
回复
在while处设置一个断点,然后单步调试下看看能不能往下走
赵4老师 2015-07-22
  • 打赏
  • 举报
回复
_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

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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