• 正文
  • 相关推荐
申请入驻 产业图谱

飞凌嵌入式ElfBoard-系统IO接口之关闭文件

10/30 11:33
257
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

1.close

用于关闭某个已打开的文件。

2.头文件

#include <unistd.h>

3.函数原型

int close(int fd);

4.参数

fd:表示要操作文件的文件描述符。

5.返回值

若关闭成功,则返回0;若失败,返回-1。

6.示例:(打开test文件,再关闭)

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <unistd.h>

#include <stdio.h>

int main()

{

int ret;

int fd = open("./test", O_RDWR);

if (fd < 0) {

printf("error: test openn");

return -1;

}

ret = close(fd);

if (ret?< 0) {

printf("error: test closen");

return -1;

}

if (ret?=?0) {

printf("succeed: test closen");

return 0;

}

}

7.编译运行并查看测试结果

succeed: test close

相关推荐