c 基础问题

niceteamwork 2015-06-05 09:49:38
调用 fopen(xxxx, "at") 方式打开了一个文件,往里面写数据,结果如果我要复制这个文件,但是提示文件被占用,然后我要打开这个文件,发现还是文件被占用,而且无论我写多少,文件大小还是显示为 0(我已经调用了fflush了)

请问,该怎么解除对这个文件的占用并能继续在里面写数据,还能保证写多少就能看到多少呢(而不是等控制台关闭了才能看到)??
...全文
384 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-06-07
  • 打赏
  • 举报
回复
fflush Flushes a stream. int fflush( FILE *stream ); Function Required Header Compatibility fflush <stdio.h> ANSI, Win 95, Win NT 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 fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error. Note If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions. Parameter stream Pointer to FILE structure Remarks The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream. Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program’s object files with COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ. For information about controlling the commit-to-disk feature, see Stream I/O, fopen, and _fdopen. Example /* FFLUSH.C */ #include <stdio.h> #include <conio.h> void main( void ) { int integer; char string[81]; /* Read each word as a string. */ printf( "Enter a sentence of four words with scanf: " ); for( integer = 0; integer < 4; integer++ ) { scanf( "%s", string ); printf( "%s\n", string ); } /* You must flush the input buffer before using gets. */ fflush( stdin ); printf( "Enter the same sentence with gets: " ); gets( string ); printf( "%s\n", string ); } Output Enter a sentence of four words with scanf: This is a test This is a test Enter the same sentence with gets: This is a test This is a test Stream I/O Routines See Also fclose, _flushall, setvbuf _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
wxhcyy 2015-06-06
  • 打赏
  • 举报
回复
引用 14 楼 wxhcyy 的回复:
你把 fopen(xxxx, "at")改成 fopen(xxxx, "at+")试试
对不起 弄错了
wxhcyy 2015-06-06
  • 打赏
  • 举报
回复
你把 fopen(xxxx, "at")改成 fopen(xxxx, "at+")试试
niceteamwork 2015-06-05
  • 打赏
  • 举报
回复
引用 7 楼 u012293844 的回复:
[quote=引用 4 楼 niceteamwork 的回复:] [quote=引用 3 楼 paschen 的回复:] 有打开就有关闭,不然系统怎么知道你程序什么时候不想用这个文件,所以加上fclose
但是我 fclose 了还能再继续写入数据么?我想一边写数据,一边可以随时打开该文件进行查看,查看的是最新的数据, 肿么办?[/quote]打开 写数据 关闭 查看 打开写数据 关闭 查看 这样做吧![/quote] 请想问问 log4cxx 写日志的时候是不是这么做的?
只此冒泡君 2015-06-05
  • 打赏
  • 举报
回复
引用 4 楼 niceteamwork 的回复:
[quote=引用 3 楼 paschen 的回复:] 有打开就有关闭,不然系统怎么知道你程序什么时候不想用这个文件,所以加上fclose
但是我 fclose 了还能再继续写入数据么?我想一边写数据,一边可以随时打开该文件进行查看,查看的是最新的数据, 肿么办?[/quote]打开 写数据 关闭 查看 打开写数据 关闭 查看 这样做吧!
自信男孩 2015-06-05
  • 打赏
  • 举报
回复
写入的还在内存里或者缓存里,需要fclose才能刷到磁盘上。所以需要先调用fclose操作。
jiqiang01234 2015-06-05
  • 打赏
  • 举报
回复
引用 4 楼 niceteamwork 的回复:
[quote=引用 3 楼 paschen 的回复:] 有打开就有关闭,不然系统怎么知道你程序什么时候不想用这个文件,所以加上fclose
但是我 fclose 了还能再继续写入数据么?我想一边写数据,一边可以随时打开该文件进行查看,查看的是最新的数据, 肿么办?[/quote] 写入的就是内存中最新的数据,直接查看内存就行了,为什么还要打开文件查看最新的呢?
niceteamwork 2015-06-05
  • 打赏
  • 举报
回复
引用 3 楼 paschen 的回复:
有打开就有关闭,不然系统怎么知道你程序什么时候不想用这个文件,所以加上fclose
但是我 fclose 了还能再继续写入数据么?我想一边写数据,一边可以随时打开该文件进行查看,查看的是最新的数据, 肿么办?
paschen 2015-06-05
  • 打赏
  • 举报
回复
有打开就有关闭,不然系统怎么知道你程序什么时候不想用这个文件,所以加上fclose
jiqiang01234 2015-06-05
  • 打赏
  • 举报
回复
先关闭,再进行其他操作
FightForProgrammer 2015-06-05
  • 打赏
  • 举报
回复
你还没有close文件句柄怎么又去打开它
Blue16。 2015-06-05
  • 打赏
  • 举报
回复
加fclose
super_admi 2015-06-05
  • 打赏
  • 举报
回复
楼主想用共享方式打开文件,这事我用C#干过,但没用C/C++干过。
todo9351 2015-06-05
  • 打赏
  • 举报
回复
可以边写边读,跟管道似的。你也不用一直 fflush 。不要用流操作吧,用 open write read close 操作。 open 的时候,加一个 O_SYNC 标志就可以同步了,不需要fflush。
Heart09 2015-06-05
  • 打赏
  • 举报
回复
我怎么记得对同一文件进行操作的时候,可以一边写一边读的呢? 而且一边写文件,一边读取文件,本来就是一种数据处理的工作方式,跟关闭不关闭这个文件有毛线关系啊? 你把你的代码关键部分贴出来(别贴太多) 我的工作环境是Linux。 ------------------------- 个人见解。 Talk is cheap, show me the code!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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