• 1.3 fclose


    http://blog.csdn.net/qin9r3y/article/details/8652207

    /*  了解C语言的存储*/

    前一节fopen的返回值FILE *类型的指针是放在堆上

    如果一个函数的返回值是指针并且有逆操作的时候,那么返回值的指针一定是在堆上的

    如果一个函数的返回值是指针没有逆操作的时候,那么返回值的指针可能是在堆上也可能在静态区

    int fclose(FILE *fp);

    参数:流

    返回值:成功0 ; 失败EOF默认为-1。

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    int main(void)
    {
        FILE *fp;
    
        fp = fopen("temp","w+");
        if( fp == NULL)
        {
            //fprintf(stderr , "fopen() failed . errno = %d
    ",errno);
            //perror("fopen()");
            fprintf(stderr,"fopen():%s
    ",strerror(errno));
            exit(1);
    
        }
        printf("ok!");
        fclose(fp);
        exit(0);
    }
    View Code

    运行结果:成功创建文件并打印OK。

    查看创建的文件权限:

    -rw-rw-r--: 0666 & ~umask(0002) ;//目的为了产生文件过松的文件

    测试使用的流资源都是有一个上限

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    int main(void)
    {
        FILE *fp;
        int count ;
        while (1)
        {
            fp = fopen("temp","w+");
            if( fp == NULL)
            {
                //fprintf(stderr , "fopen() failed . errno = %d
    ",errno);
                perror("fopen()");
                break ;
            }
        }
        printf("count = %d
    ",count);
        exit(0);
    }

    查看输出结果:

    fopen(): Too many open files
    count = 1021

    命令:ulimit -a  查看---open files    1021+stdin+stderr+stdout ==1024

  • 相关阅读:
    例解 Linux 下 Make 命令
    linux使用bin文件安装jdk
    Linux查看及设置系统字符集
    FTP的两种主动模式和被动模式
    Mongodb之主从复制
    Nginx配置认证登录
    AWK
    Redis+Keepalived实现高可用
    Redis哨兵配置
    Keepalived指定文件接收日志
  • 原文地址:https://www.cnblogs.com/muzihuan/p/4771947.html
Copyright © 2020-2023  润新知