close.c

来自「部分linux c语言函数」· C语言 代码 · 共 37 行

C
37
字号
/****************************************************************相关的头文件:#include<unistd.h>函数表达式  :int close(int fd);参数说明    :close函数的参数fd表示需要关闭的文件的文件描述符。返回值说明  :成功关闭文件则返回0,失败则返回-1。函数功能详解:略****************************************************************/#include<stdio.h>#include<stdlib.h>#include<unistd.h>  /*close函数所在头文件*/#include<fcntl.h>  /*open函数所在头文件*/int main(void){    int fd;    /*文件描述符,成功则返回0,失败则返回-1*/    fd=open("test.txt",O_RDWR|O_CREAT,0700);  /*打开一个文件*/    if(fd == -1)    {        perror("fail to open");  /*出错则输出出错信息*/        exit(1);    }    else        printf("open OK\n");     /*成功打开文件则输出提示信息*/    if(close(fd)==-1)    /*关闭打开的文件*/    {        perror("fail to close");   /*出错则输出信息*/        exit(1);    }    else        printf("close OK\n");     /*成功关闭文件则输出提示信息*/    return 0;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?