Linux文件编程demo6文件打开创建的补充.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main
{
	//通过O_EXCL判断文件是否已经存在和O_CREAT一起使用
	int fd;          
	fd = open("./file1",O_RDWR|O_CREAT|O_EXCL);
	if(fd == -1){
		printf("文件已经创建\n");
	}
	
	//通过O_APPEND在文件原来内容后面追加内容
	int fd2
	char *buf = "longhaiyanghenshuai!";
	fd2 = open("./file1",O_RDWR|O_APPEND);
    int n_write = write(fd2,buf,strlen(buf));
	printf("write %d byte to file1,context:%s\n",n_write,buf);
	close(fd2);
	
	//通过O_TRUNC覆盖原来的内容
	int fd3;
	char *buf = "test";
	fd3 = open("./file1",O_RDWR|O_TRUNC);
	int n_write = write(fd,buf,strlen(buf));
	printf("write %d byte to file1,context:%s\n",n_write,buf);
	close(fd3);
	
	
	//创建文件的函数creat
	/*常见的创建模式:
	1.S_IRUSR  4 可读
	2.S_IWUSR  2 可写
	3.S_IXUSR  1 可执行
	4.S_IRWXU  7 读写执行
	*/
	
	int fd4;
	fd4 = creat("./longhaiyang",S_IRWXU);//在当前目录下创建文件 权限读写执行
	
	return 0;
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值