open函数与close函数

1.open()函数

头文件:
#include <fcntl.h>//在centos6.0中只要此头文件就可以
#include <sys/types.h>
#incldue <sys/stat.h>
功能:打开和创建文件(建立一个文件描述符,其他的函数可以通过文 件描述符对指定文件进行读取与写入的操作。)

原型

int open(const char*pathname,int flags);
int open(const char*pathname,int flags,mode_t mode);
参数说明:
1.pathname
  要打开或创建的目标文件
2.flags
  打开文件时,可以传入多个参数选项,用下面的
  一个或者多个常量进行“或”运算,构成falgs
  参数:
  O_RDONLY:   只读打开
  O_WRONLY:   只写打开
  O_RDWR:     读,写打开
这三个常量,必须制定一个且只能指定一个
  O_CREAT:    若文件不存在,则创建它,需要使
              用mode选项。来指明新文件的访问权限
  O_APPEND:   追加写,如果文件已经有内容,这次打开文件所
              写的数据附加到文件的末尾而不覆盖原来的内容
              

ps:open函数具体使用那个,和具体应用场景相关,如目标文件存在,使用两个参数的open,如果目标文件不存在,需要open创建,则第三个参数表示创建文件的默认权限

返回值

成功:新打开的文件描述符
失败:-1
open返回的文件描述符一定是最小的而且没有被使用的

fopen与open的区别

以可写的方式fopen一个文件时,如果文件不存在则会自动创建,而open一个文件时必须明确O_CREAT才会创建文件,否则文件不存在就出错返回

2.close()函数

头文件:#include<unistd.h>
功能:关闭一个已经打开的文件

原型

int close(int fd)
参数说明:
 fd:是需要关闭的文件描述符

返回值

成功:返回0;
失败:返回-1,并设置errno

打开的文件描述符一定要记得关闭,否则资源会被大量的占用,导致内存不够

3.open打开存在的文件

示例1:

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
	int fd=open("myfile.txt",O_WRONLY);//需要准备myfile.txt文件
	if(fd<0)
	{
		perror("open");
		exit(1);
	}
	const char*msg="hello open\n";
	int count = 6;
	while(count--)
	{
		write(fd,msg,strlen(msg));
	}
	close(fd);
	return 0;
}

运行结果:
在这里插入图片描述
示例2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
	int fd=open("myfile.txt",O_RDWR);
	if(fd<0)
	{
		perror("open");
		exit(1);
	}
	const char*msg="hello  hahaha\n";
	int count= 10;
	while(count--)
	{
		write(fd,msg,strlen(msg));
	}
	char buf[1024]= {0};
	int num=10;
	while(num--)
	{
		read(fd,buf,strlen(msg));
	}
	close(fd);
	return 0;
}

运行结果:
在这里插入图片描述

3.open打开不存在的文件

示例1:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

int main()
{
	int fd=open("file.txt",O_WRONLY|O_CREAT,0644); //file文件不存在,所以在书写第二个参数时,记得把O_CREAT加上, //如果不加O_CREAT的话,程序就会报此文件不存在
	if(fd<0)
	{
		perror("open");
		exit(1);
	}
	const char*msg="hello file\n";
	int count=10;
	while(count--)
	{
		write(fd,msg,strlen(msg));
	}
	close(fd);
	return 0;
}

运行结果:
在这里插入图片描述
参考资料:
https://blog.csdn.net/dangzhangjing97/article/details/79631173

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
open函数openat函数都是用于打开文件的系统调用,但它们有一些区别。 1. 参数传递方式: - open函数接受一个文件路径名作为参数,该路径名可以是绝对路径或相对路径。 - openat函数需要传递一个已打开的目录文件描述符(dirfd)和一个相对于该目录的路径名作为参数。这种方式可以更加灵活地控制文件的打开位置。 2. 目录解析方式: - open函数的文件路径名会根据当前进程的工作目录进行解析。 - openat函数的路径名是相对于提供的目录文件描述符(dirfd)进行解析。 3. 安全性考虑: - open函数在解析文件路径时,依赖于进程的当前工作目录。这可能会导致安全性问题,特别是在多线程环境下,因为当前工作目录是共享的。 - openat函数提供了更安全的方式,可以避免依赖于进程的当前工作目录,而是通过提供目录文件描述符来指定相对路径。 使用示例: 以下是使用open函数openat函数打开文件的示例代码: 使用open函数打开文件: ```c #include <fcntl.h> int fd = open("/path/to/file", O_RDWR); if (fd == -1) { // 打开文件失败 } // 在文件中进行读写操作 ``` 使用openat函数打开文件: ```c #include <fcntl.h> #include <unistd.h> int dirfd = open("/path/to/directory", O_RDONLY); if (dirfd == -1) { // 打开目录失败 } int fd = openat(dirfd, "file.txt", O_RDONLY); if (fd == -1) { // 打开文件失败 } // 在文件中进行读操作 close(fd); close(dirfd); ``` 需要注意的是,使用完打开的文件描述符后,需要调用close函数关闭文件描述符,以释放资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值