user-0 提问: c 基础问题

回复:
user: user-1
content: 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

user: user-2
content: 引用 14 楼 wxhcyy 的回复:你把 fopen(xxxx, "at")改成 fopen(xxxx, "at+")试试 对不起 弄错了

user: user-2
content: 你把 fopen(xxxx, "at")改成 fopen(xxxx, "at+")试试

user: user-0
content: 引用 7 楼 u012293844 的回复:[quote=引用 4 楼 niceteamwork 的回复:] [quote=引用 3 楼 paschen 的回复:] 有打开就有关闭，不然系统怎么知道你程序什么时候不想用这个文件，所以加上fclose  但是我 fclose 了还能再继续写入数据么？我想一边写数据，一边可以随时打开该文件进行查看，查看的是最新的数据， 肿么办？[/quote]打开 写数据 关闭 查看  打开写数据 关闭 查看  这样做吧！[/quote]  请想问问 log4cxx 写日志的时候是不是这么做的？

user: user-3
content: 引用 4 楼 niceteamwork 的回复:[quote=引用 3 楼 paschen 的回复:] 有打开就有关闭，不然系统怎么知道你程序什么时候不想用这个文件，所以加上fclose  但是我 fclose 了还能再继续写入数据么？我想一边写数据，一边可以随时打开该文件进行查看，查看的是最新的数据， 肿么办？[/quote]打开 写数据 关闭 查看  打开写数据 关闭 查看  这样做吧！

user: user-4
content: 写入的还在内存里或者缓存里，需要fclose才能刷到磁盘上。所以需要先调用fclose操作。

user: user-5
content: 引用 4 楼 niceteamwork 的回复:[quote=引用 3 楼 paschen 的回复:] 有打开就有关闭，不然系统怎么知道你程序什么时候不想用这个文件，所以加上fclose  但是我 fclose 了还能再继续写入数据么？我想一边写数据，一边可以随时打开该文件进行查看，查看的是最新的数据， 肿么办？[/quote] 写入的就是内存中最新的数据，直接查看内存就行了，为什么还要打开文件查看最新的呢？

user: user-0
content: 引用 3 楼 paschen 的回复:有打开就有关闭，不然系统怎么知道你程序什么时候不想用这个文件，所以加上fclose  但是我 fclose 了还能再继续写入数据么？我想一边写数据，一边可以随时打开该文件进行查看，查看的是最新的数据， 肿么办？

user: user-6
content: 有打开就有关闭，不然系统怎么知道你程序什么时候不想用这个文件，所以加上fclose

user: user-5
content: 先关闭，再进行其他操作

user: user-7
content: 你还没有close文件句柄怎么又去打开它

user: user-8
content: 加fclose

user: user-1
content: http://blog.chinaunix.net/uid-26430381-id-4200682.html

user: user-9
content: 楼主想用共享方式打开文件，这事我用C#干过，但没用C/C++干过。

user: user-10
content: 可以边写边读，跟管道似的。你也不用一直 fflush 。不要用流操作吧，用 open  write read close 操作。 open 的时候，加一个 O_SYNC 标志就可以同步了，不需要fflush。

user: user-11
content: 我怎么记得对同一文件进行操作的时候，可以一边写一边读的呢？ 而且一边写文件，一边读取文件，本来就是一种数据处理的工作方式，跟关闭不关闭这个文件有毛线关系啊？ 你把你的代码关键部分贴出来（别贴太多） 我的工作环境是Linux。   ------------------------- 个人见解。 Talk is cheap, show me the code!