MADN上的一些基础内容:
C++ Library Overview
File Handling
Run-Time Routines by Category:
Data Conversion
Input and Output
Memory Allocation
Process and Environment Control
Searching and Sorting
Text and Binary Mode File I/O
Stream I/O (fopen, fclose, fseek, ftell, fread, rewind, fwrite, tmpfile, feof: in <cstdio>)
_fileno -> Get file handle associated with stream
fsetpos -> Sets the stream-position indicator.
fread
Reads data from a stream.
size_t fread( void *buffer, size_t size, size_t count, FILE *stream );
Return Value
fread returns the number of full items actually read, which may be less than count if an error occurs or if the end of the file is encountered before reaching count. Use the feof or ferror function to distinguish a read error from an end-of-file condition. If size or count is 0, fread returns 0 and the buffer contents are unchanged.
Parameters
buffer ->Storage location for data
size ->Item size in bytes
count ->Maximum number of items to be read
stream ->Pointer to FILE structure
fwrite
Writes data to a stream.
size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );
Return Value
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.
Parameters
buffer -> Pointer to data to be written
size -> Item size in bytes
count -> Maximum number of items to be written
stream -> Pointer to FILE structure
fopen, _wfopen
Open a file.
FILE *fopen( const char *filename, const char *mode ); <stdio.h>
FILE *_wfopen( const wchar_t *filename, const wchar_t *mode ); <stdio.h> or <wchar.h>
Return Value
Each of these functions returns a pointer to the open file. A null pointer value indicates an error.
Parameters
filename -> Filename
mode -> Type of access permitted
Remarks
The fopen function opens the file specified by filename. _wfopen is a wide-character version of fopen; the arguments to _wfopen are wide-character strings. _wfopen and fopen behave identically otherwise.
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 fopen 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) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t 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 written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
"b" -> Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.
"t" -> Open in text (translated) mode
_fmode
The _fmode variable sets the default file-translation mode for text or binary translation. It is declared in STDLIB.H as
extern int _fmode;
The default setting of _fmode is _O_TEXT for text-mode translation. _O_BINARY is the setting for binary mode.
fclose, _fcloseall
Closes a stream (fclose) or closes all open streams (_fcloseall).
int fclose( FILE *stream );
Return Value
fclose returns 0 if the stream is successfully closed. _fcloseall returns the total number of streams closed. Both functions return EOF to indicate an error.
Parameter
stream -> Pointer to FILE structure
fseek
Moves the file pointer to a specified location.
int fseek( FILE *stream, long offset, int origin );
Return Value
If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.
Parameters
stream -> Pointer to FILE structure
offset -> Number of bytes from origin
origin -> Initial position
Remarks
The fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H:
SEEK_CUR -> Current position of file pointer
SEEK_END -> End of file
SEEK_SET -> Beginning of file
ftell
Gets the current position of a file pointer.
long ftell( FILE *stream );
Parameter
stream -> Target FILE structure
rewind
Repositions the file pointer to the beginning of a file.
void rewind( FILE *stream );
Return Value
None
Parameter
stream -> Pointer to FILE structure
Remarks
The rewind function repositions the file pointer associated with stream to the beginning of the file. A call to rewind is similar to
(void) fseek( stream, 0L, SEEK_SET );
However, unlike fseek, rewind clears the error indicators for the stream as well as the end-of-file indicator. Also, unlike fseek, rewind does not return a value to indicate whether the pointer was successfully moved.
To clear the keyboard buffer, use rewind with the stream stdin, which is associated with the keyboard by default.
feof
Tests for end-of-file on a stream.
int feof( FILE *stream );
Return Value
The feof function returns a nonzero value after the first read operation that attempts to read past the end of the file. It returns 0 if the current position is not end of file. There is no error return.
Parameter
stream -> Pointer to FILE structure
Remarks
The feof routine (implemented both as a function and as a macro) determines whether the end of stream has been reached. When end of file is reached, read operations return an end-of-file indicator until the stream is closed or until rewind, fsetpos, fseek, or clearerr is called against it.
在C中的头文件用 .h 表示;
一些常用的字符格式处理函数
sprintf, swprintf ;sscanf, swscanf ;_snprintf, _snwprintf
ftell 的头文件为<stdio.h>,其类似操作的有_tell, _telli64 的头文件为<io.h>;
一般文件操作包含的头文件 <stdio.h>、<io.h>、<fcntl.h>。
_filelength, _filelengthi64根据文件句柄返回文件长度;stdio.h中应该有一个类似的 filelength吧?