fopen函数

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     }

 更新于

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值