linux高编IO-------fclose

本文详细介绍了C语言中文件操作的基础知识,特别是针对fopen和fclose函数的使用进行了深入探讨。通过实例展示了如何打开和关闭文件,以及在操作过程中可能出现的问题和解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/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\n",errno);
        //perror("fopen()");
        fprintf(stderr,"fopen():%s\n",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\n",errno);
            perror("fopen()");
            break ;
        }
    }
    printf("count = %d\n",count);
    exit(0);
}

查看输出结果:

fopen(): Too many open files
count = 1021

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

 

转载于:https://www.cnblogs.com/muzihuan/p/4771947.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值