close函数,用来关闭已打开的文件.
(1).语法
int close(int fd)
说明:该函数用来关闭已打开的文件.指定的参数fd为open()或creat()打开的文件
描述符.
返回值:关闭成功返回0,失败则返回-1.
详解:close()函数用来关闭一个已打开的文件,并且将文件修改过的内容写回磁盘
任何有关该文件描述符上的记录锁,和使用该文件的进程都会被关闭和移除
实例:
#include<stdio.h>
#include<stdlib.h>
#include<fcntl.h>
#include<unistd.h>
int main(void)
{
int fd;
fd=open("test.txt",O_RDWR|O_CREAT,00777);
if(fd<0)
{
printf("Faile to open file\n");
exit(1);
}
else
{
printf("Success to open file\nFile Description is %d\n",fd);
if(close(fd)==0)
{
printf("Success to close file\n");
}
else
{
printf("Faile to close file\n");
}
}
return 0;
}
转载于:https://blog.51cto.com/huangyandong/677922