No manual entry for fopen
如果出现这个错误,可以尝试yum install man-pages,因为默认的manual不完整。
$ man fopen
#include <stdio.h>
FILE *fopen(const char *pathname, const char *mode);
fopen 函数的定义使用了常量指针(const char*),因此可以放心使用,(Linux系统会对试图修改常量的行为报错)
返回值解析:
$/RET
...skipping...
RETURN VALUE
Upon successful completion fopen(), fdopen() and freopen() return a FILE pointer.
Otherwise, NULL is returned and errno is set to indicate the error.
如果成功则返回FILE指针,否则返回NULL,errno被设置来表明错误
errno是一个宏;
The argument mode points to a string beginning with one of the following
sequences (possibly followed by additional characters, as described below):
这个mode只能识别一个串开头的内容,如:readwrite不会报错,但只会识别r。
r Open text file for reading. The stream is positioned at the begin‐
ning of the file.
文件以只读形式打开。文件位置指针定位在文件开头(从头开始读文件);
r+ Open for reading and writing. The stream is positioned at the
beginning of the file.
文件以读写形式打开,从头开始读文件;
w Truncate file to zero length or create text file for writing. The
stream is positioned at the beginning of the file.
有文件则截断成零长度(清空),无文件则以只写形式创建
w+ Open for reading and writing. The file is created if it does not
exist, otherwise it is truncated. The stream is positioned at the
beginning of the file.
以读写打开,无则创建,有则清空,
a Open for appending (writing at end of file). The file is created if
it does not exist. The stream is positioned at the end of the file.
a+ Open for reading and appending (writing at end of file). The file
is created if it does not exist. The initial file position for
reading is at the beginning of the file, but output is always
appended to the end of the file.
注意:1、如果程序需要移植到windows,则需要加b,表示二进制流。
2、报错的俩个函数:
perror(),
stderror(),注意包括string.h
3、r模式不会创建文件,如果文件不存在则会报错。
4、fopen的返回值在堆上,fclose里面会有free操作来释放堆;
6 ************************************************************************/
7 //统计文件字符数
8 #include<stdio.h>
9 #include<stdlib.h>
10 #include<string.h>
11 int main(int argc,char **argv)
12 {
13 FILE *fp;
14 int count=0;
15 if(argc<2)
16 {
17 fprintf:(stderr,"Usage:....\n");
18 exit(1);
19 }
20 fp=fopen(argv[1],"r");
21 if(fp== NULL)
22 {
23 perror("fopen()");
24 exit(1);
25 }
更新于