c 程序自动打开3个文件,标准输入stdin,标准输出stdout和标准错误stderr输出。
FILE是stdio.h中定义的一种派生类型。指针并不指向实际的文件,而是指向一个关于文件的信息的数据包,其中包括文件I/O使用的缓冲区信息。
int fgetc ( FILE * stream ); 返回读取的字符。
Returns the character currently pointed by the internal file position indicator of
the specified stream. The internal file position indicator is then advanced by one
character to point to the next character.
fgetc and getc are equivalent, except that the latter one may be implemented as a macro.
------------------------------
int fputc ( int character, FILE * stream );
Writes a character to the stream and advances the position indicator.
The character is written at the current position of the stream as indicated by
the internal position indicator, which is then advanced one character.
-----------------------
exit()函数关闭所有打开的文件并终止程序。在stdlib.h头文件中有声明。
在最初调用的main()中使用return和调用exit()的效果相同。
但在不是最初调用的那一级,exit()会终止程序,但return将控制权移交给递归的前一级。
-----------------------
FILE * fopen ( const char * filename, const char * mode );
C string containing a file access modes. It can be:
"r" Open a file for reading. The file must exist. "w" Create an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a" Append to a file. Writing operations append data at the end of the file. The file is created if it does not exist. "r+" Open a file for update both reading and writing. The file must exist. "w+" Create an empty file for both reading and writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. "a+" Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.
上面的模式是以文本模式打开文件,再加上'b',成为:"rb", "wb", "ab", "r+b", "w+b", "a+b","rb+", "wb+", "ab+",这些代表用二进制模式打开文件。