1、LibrariesLIBC.LIBSingle thread static library, retail versionLIBCMT.LIBMultithread static library, retail versionMSVCRT.LIBImport library for MSVCRT.DLL, retail versionThe c, n, and t mode options are Microsoft extensions for fopen and _fdopen and should not be used where ANSI portability is desired
2、.Return ValueEach of these functions returns a pointer to the open file. A null pointer value indicates an error. ParametersfilenameFilenamemodeType of access permittedRemarksThe fopen function opens the file specified by filename. _wfopen is a wide-character version of fopen; the arguments to _wfop
3、en are wide-character strings. _wfopen and fopen behave identically otherwise.Generic-Text Routine MappingsTCHAR.H Routine _UNICODE & _MBCS Not Defined_MBCS Defined_UNICODE Defined_tfopenThe character string mode specifies the type of access requested for the file, as follows:rOpens for reading. If
4、the file does not exist or cannot be found, the fopen call fails.wOpens an empty file for writing. If the given file exists, its contents are destroyed.aOpens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if
5、 it doesnt 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; the appending operation includes the removal of the EOF marker before new data is w
6、ritten to the file and the EOF marker is restored after writing is complete;When a file is opened with the or 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 o
7、peration is carried out. Thus, existing data cannot be overwritten.The mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The mode does remove the
8、 EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.When the , , or access type is specified, both reading and writing are allowed (the file is
9、 said to be open for “update”). However, when you switch between reading and writing, there must be an intervening fflush, 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, the following characte
10、rs can be included in mode to specify the translation mode for newline characters:tOpen in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with , fopen checks for a CTRL+Z at the end of the file and removes it, if
11、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. Also, in text mode, carriage returnlinefeed combinations are translated into single linefeeds on input, and linefeed characters are trans
12、lated to carriage returnlinefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide ch
13、aracters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).bOpen in binary (untranslated) mode; translations involving carriage-return and linefeed characters are
14、 suppressed. If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL. For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Bin
15、ary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.cEnable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.nReset the commit flag for the associated filename to “no-commit.” This i
16、s the default. It also overrides the global commit flag if you link your program with COMMODE.OBJ. The global commit flag default is “no-commit” unless you explicitly link your program with COMMODE.OBJ.Valid characters for the mode string used in fopen and _fdopen correspond to oflag arguments used
17、in _open and _sopen, as follows.Characters in mode StringEquivalent oflag Value for _open/_sopena_O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND)a+_O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT )r_O_RDONLYr+_O_RDWRw_O_WRONLY (usually _O_WRONLY | _O_CREAT | _O_TRUNC)w+_O_
18、RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC)_O_BINARY_O_TEXTNoneExample/* FOPEN.C: This program opens files named data * and data2.It uses fclose to close and * _fcloseall to close all remaining files. */#include FILE *stream, *stream2;void main( void ) int numclosed; /* Open for read (will fail if
19、file does not exist) */ if( (stream = fopen( ) = NULL ) printf( The file data was not openedn ); else was openedn /* Open for write */ if( (stream2 = fopen( data2 /* Close stream */ if( fclose( stream ) ) was not closedn /* All other files are closed: numclosed = _fcloseall( );Number of files closed
20、 by _fcloseall: %un, numclosed );Output was opened 1Stream I/O RoutinesSee Alsofclose, _fdopen, ferror, _fileno, freopen, _open, _setmodefclose, _fcloseallCloses a stream (fclose) or closes all open streams (_fcloseall).int fclose( FILE *stream );int _fcloseall( void );fclose_fcloseallfclose returns
21、 0 if the stream is successfully closed. _fcloseall returns the total number of streams closed. Both functions return EOF to indicate an error.ParameterstreamPointer to FILE structureThe fclose function closes stream. _fcloseall closes all open streams except stdin, stdout, stderr (and, in MS-DOS, _
22、stdaux and _stdprn). It also closes and deletes any temporary files created by tmpfile. In both functions, all buffers associated with the stream are flushed prior to closing. System-allocated buffers are released when the stream is closed. Buffers assigned by the user with setbuf and setvbuf are no
23、t automatically released. _close, _fdopen, fflush, fopen, freopenfwriteWrites data to a stream.size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined.bufferPointer to data to be writtensizeItem size in bytescountMaximum number of items to be writtenThe fwrite funct
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1