函数原型:
int close(int fd);
作用:
close( )用来关闭open( )打开的文件。
返回值:
若关文件动作成功则返回0,有错误发生时则返回EOF并把错误代码存到errno
1、EBADF 表示参数 fd 非已打开的文件的文件描述符
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h> // close 的头文件
#include <fcntl.h>
int main(int argc, char const *argv[])
{
if (argc != 2)
{
printf("请输入文件名!\n");
exit(1);
}
int fd = open(argv[1], O_RDWR | O_CREAT, 0655);
if (fd == -1)
{
perror("open file error");
}
printf("fd = %d\n", fd);
/***********************************************************************/
close(fd);
return 0;
}
该程序演示了如何使用close()函数来关闭open()打开的文件。如果文件描述符fd无效,close()将返回EBADF。在成功关闭文件后,它返回0。

1万+

被折叠的 条评论
为什么被折叠?



