文件操作函数fopen()使用笔记
2011-11-15 22:02
483 查看
FILE *fopen( const char *filename, const char *mode );
Parameters
filename Filename mode Type of access permittedReturn Values
Each of these functions returns a pointer to the open file. A null pointer value indicates an error.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.
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 usingfseek, but is always moved back to the end of the file before any write operation is carried
out. Thus, existing data cannot be overwritten.
The "a" 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 "a+" mode does remove the EOF marker
before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.
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 you switch between reading and writing, there must be an interveningfflush,fsetpos,
or fseek operation. The current position can be specified for thefsetpos orfseek operation, if desired.
In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters:
t Open 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 "a+",fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is
done because usingfseek and ftell to move within a file that ends with a CTRL+Z, may causefseek to behave improperly near the end of the file.
Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed 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 characters. For the same reason, the Unicode stream-output functions convert
wide characters to multibyte characters.
b Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are 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 Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.
c
Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if eitherfflush or_flushall is called.
n
Reset the commit flag for the associated filename to “no-commit.” This is the default.
Example
/* FOPEN.C: This program opens files named "data" * and "data2".It uses fclose to close "data" and * _fcloseall to close all remaining files. */ #include <stdio.h> FILE *streamData1; /* First pointer to the open file: Data1 */ FILE *streamData2; /* Second pointer to the open file: Data2 */ int main( int argc, char* argv[] ) { int numOfClosedFile; /* Use _fcloseall() to close all remaining files */ /* Open for read (will fail if file "data1" does not exist) */ /* "r" -- Opens for reading. If the file does not exist or cannot be found, the fopen call fails. */ if( (streamData1 = fopen( "F:\\WENDUJIEXI\\mysort\\data1", "r" )) == NULL ) printf( "The file 'data' was not opened\n" ); else printf( "The file 'data' was opened\n" ); /* Open for write */ /* Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed. */ if( (streamData2 = fopen( "F:\\WENDUJIEXI\\mysort\\data2", "w+" )) == NULL ) printf( "The file 'data2' was not opened\n" ); else printf( "The file 'data2' was opened\n" ); /* Close streamData1 */ if( fclose( streamData1 ) ) printf( "The file 'data' was not closed\n" ); else printf( "The file 'data' was closed successfully!\n" ); /* All other files are closed: */ numOfClosedFile = _fcloseall( ); printf( "Number of files closed by _fcloseall(): %u\n", numOfClosedFile ); return 0; }
调试结果:
(1) 目录 F:\WENDUJIEXI\mysort\下最开始只有data1文件,而没有data2文件。
其中data1文件的内容为:12345.
(2) 执行上述程序之后,data1文件内容还是12345;自动创建data2文件,内容为空。
(3) 如果最开始目录下面就已经存在data2文件,而且内容为:123456789;那么执行外上述程序之后文件内容的变为:
data1——依然为12345
data2——变为空
最开始文件存在如下:
data2的内容:
执行完之后,data2内容变成了空:
相关文章推荐
- 使用PHP文件操作函数fopen()遇到的问题
- 使用文件操作函数实现简单的CP、cat、size功能及读取文件内容初始化结构体
- STM32学习笔记之fatfs文件系统接口函数使用
- 使用Windows的SHFileOperation外壳函数实现文件操作
- Go学习笔记二: 函数,文件操作及网络通信
- 笔记--python列表及元组以及关于列表操作的函数使用
- c语言中所有文件操作函数详解fopen、fwrite、fread、fgetc、fputc、fscanf、fprintf、ftell、fseek等函数
- PHP文件系统函数详细使用与操作(一)
- PHP中的一些文件操作函数使用
- PHP使用fopen()函数打开文件提示权限不够问题
- C语言几个文件操作函数fopen(),fseek(),fscanf(),fwrite()
- Qt模块化笔记之core——Input/Output文件操作-流QDataStream使用
- 操作 Wave 文件(11): 使用 waveIn...函数录制 wav 文件
- PHP下使用fopen函数打开文件时的几点注意
- 文件操作之fprintf函数的使用
- JS学习笔记1——不要使用JavaScript内置的parseInt()和Number()函数,利用map和reduce操作实现一个string2int()函数
- Matlab使用函数解析之文件操作
- 树莓派学习笔记——使用文件IO操作GPIO SysFs方式
- PHP操作文件的一些基本函数使用示例
- [Linux流操作]使用fgetpos函数获取文件当前偏移量