zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    list for循环中删除元素
    XMLFeedSpider例子
    myeclipse一直卡在loading workbench解决方法
    代码
    在Github上面搭建Hexo博客(一):部署到Github
    RegEX正则表达式截取字符串
    将后台值传单前台js接收
    C# List<T>泛型用法
    基于jQuery——TreeGrid
    在线编程学习网站
  • 原文地址:https://www.cnblogs.com/muzihuan/p/4771947.html
Copyright © 2011-2022 走看看