打开/创建文件(linux系统编程)

本文详细解析了Linux系统中open函数的使用方法及其各种标志位的功能,包括O_RDWR、O_CREAT、O_EXCL、O_APPEND、O_TRUNC等,并通过实例代码展示了如何创建、读写和控制文件。
  • open函数

参数说明在这里插入图片描述

  • demo1代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
	int fd;
	//int open(const char *pathname, int flags);
        

	fd=open("./file1",O_RDWR);//可读可写
	printf("fd=%d\n",fd);
	return 0;
}

如果事先创好文件file1(touch file1),则fd =3(我这里是3),如果之前没有创建file1,则fd=-1.
优化过后的代码(如果之前没有创建则创建)

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


int main()
{
	int fd;
	//int open(const char *pathname, int flags);
        

	fd=open("./file1",O_RDWR);//可读可写
	if(fd == -1){
		printf("创建失败\n");
		//int open(const char *pathname, int flags, mode_t mode);
		fd=open("./file1",O_RDWR|O_CREAT,0600);//如果没有file1则创建,0600:权限是可读可写
		if(fd > 0){
			printf("创建成功,fd=%d\n",fd);
		}
	}		
	return 0;
}

补充:可读 4.可写 2.执行1,6=4+2,可读可写。
— -> 0 (no excute , no write ,no read)
–x -> 1 excute, (no write, no read)
-w- -> 2 write
-wx -> 3 write, excute
r-- -> 4 read
r-x -> 5 read, excute
rw- -> 6 read, write ,
rwx -> 7 read, write , excute
**

  • O_EXCL(判断文件是否存在)
    如果同时指定CREAT,而文件已经存在,则出错。
    代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
	int fd;
	//int open(const char *pathname, int flags);
        

	
	fd=open("./file1",O_RDWR|O_CREAT|O_EXCL,0600);//如果没有file1则创建,0600:权限是可读可写
	if(fd ==-1){
		printf("文件已经存在\n");
	}
			
	return 0;
}

结果
在这里插入图片描述
O_APPEND
每次写的时候加到文件的尾端
代码

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

int main()
{
	int fd;
	int n_open;
	int n_write;
	int n_read;
	char *Buf="hello ubuntu";
	fd=open("./file7",O_RDWR);
	
	
	n_write=write(fd,Buf,strlen(Buf));
	if(n_write != -1){
		printf("写入成功,写了%d个字节\n",n_write);
	}
	
	close(fd);	
	return 0;
}

没有加O_APPEND
运行前的file7
在这里插入图片描述
运行后
在这里插入图片描述
相当于覆盖了之前写的东西(hello ubuntu 覆盖掉了1234567)
加了O_APPEND
运行前
在这里插入图片描述
运行后
在这里插入图片描述
O_TRUNC
去打开文件时,如果这个文件夹本来是有内容的,而且为只读或者只写成功打开,则见其长度截短为0。(相当于把原来的存在的直接干掉)
代码

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

int main()
{
	int fd;
	int n_open;
	int n_write;
	int n_read;
	char *Buf="hello";
	fd=open("./file7",O_RDWR|O_TRUNC);
	
	
	n_write=write(fd,Buf,strlen(Buf));
	if(n_write != -1){
		printf("写入成功,写了%d个字节\n",n_write);
	}
	
	close(fd);	
	return 0;
}

运行前
在这里插入图片描述
运行后
在这里插入图片描述
O_CREAT
在这里插入图片描述
代码

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

int main()
{
	int fd;
	//int creat(const char *pathname, mode_t mode);
	creat("./file9", S_IRWXU);//可读可写可执行
	
	
	
	
	close(fd);	
	return 0;
}

结果
在这里插入图片描述

//1.创建文件file1,写入字符串“abcdefghijklmn”; //2.创建文件file2,写入字符串“ABCDEFGHIJKLMN”; //3.读取file1中的内容,写入file2,使file2中的字符串内容为“abcdefghijklmn ABCDEFGHIJKLMN” 创建文件,该文件具有用户读写权限。 //2.采用dup/dup2/fcntl复制一个新的文件描述符,通过新文件描述符向文件写入“class_name”字符串; //3.通过原有的文件描述符读取文件中的内容,并且打印显示; 1.输入文件名称,能够判断文件类型,判断实际用户对该文件具有哪些存取权限; ?2.要求打印出文件类型信息,inode节点编号,链接数目,用户id,组id,文件大小信息; ?3.修改文件的权限为当前用户读写,组内用户读写,组外用户无权限 新建文件,设置文件权限屏蔽字为0; 2.建立该文件的硬链接文件,打印硬链接文件的inode节点号和文件大小; ? 3.建立该文件的软链接文件,打印软链接文件的inode节点号和文件大小;打印软链接文件中的内容; 4.打印源文件的inode节点号,文件大小和链接数目; ? 5.调用unlink对源文件进行操作,打印源文件链接数目; .新建/home/user目录; 2.把当前工作路径移至/home/user目录; 3.打印当前工作路径; ?编写程序完成以下功能: ?1.递归遍历/home目录,打印出所有文件和子目录名称及节点号。 ?2.判断文件类型,如果是子目录,继续进行递归遍历,直到遍历完所有子目录为止
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值