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