文件(一)

一、Linux操作系统为我们提供一系列文件的API

1. 打开——open
2. 读写——write /read
3. 光标定位——lseek
4. 关闭——close

二、打开/创建、关闭文件

  1. 只打开文件:成功返回文件描述符fd,失败返回-1
int open(const char *pathnane, int flags);	
  1. 打开文件,如果文件不存在,就创建文件
int open(const char *pathnane, int flags, node_ t mode);
  1. 创建文件
int creat(const char *pathnane, mode t mode);
  1. 关闭文件
int close(int fd);

参数解释:
Pathname:要打开的文件名(含路径,缺省为当前路径)

Flags:O_RDONLY 只读打开 O_WRONLY 只写打开 O_RDWR 可读可写打开

当我们附带了权限后,打开的文件就只能按照这种权限来操作。
以上这三个常数中应当只指定一 个,下列常数是可选择的:
O_CREAT 若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode,用其说明该新文件的存取许可权限。
O_EXCL 如果同时指定了OCREAT,而文件已经存在,则出错。
O_APPEND 每次写时都加到文件的尾端。
O_TRUNC 属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0。

在open中,mode
Mode:一定是在flags中使用了O_CREAT标志,mode记录待创建的文件的访问权限,具有以下几种:

  1. r——可读——4
  2. w——可写——2
  3. x——可执行——1

在creat中,mode
S_IRUSR——4——可读
S_IWUSR——2——可写
S_IXUSR——1——可执行
S_IRWXU——7——可读、可写、可执行

1.只打开文件

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

int main()
{
	int fd;
	fd = open("./file1", O_RDWR);	//打开当前路径下file1文件 
	if(fd == -1){
		printf("打开文件失败"); 
	}else{
		printf("打开文件成功");
	}

	return 0;
}

如果当前路径下存在file1文件
在这里插入图片描述
如果当前路径下不存在file1文件

2.打开文件,如果文件不存在并创建文件

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

int main()
{
	int fd;
	fd = open("./file1", O_RDWR);	//打开当前路径下file1文件 
	if(fd == -1){
		printf("打开文件失败\n");
		
		fd = open("./file1", O_RDWR | O_CREAT, 0600);	//打开当前路径下file1文件,如果没有就创建,创建格式可读可写
		if(fd > 0){
			printf("创建文件成功\n"); 
		}
	}	
	return 0;
}

在这里插入图片描述

3.创建文件

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

int main()
{
	int fd;

	
	fd = creat("./file", S_IRWXU);
	if(fd != -1){
		printf("创建file文件成功\n");
	}
		
	return 0;
}

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

4.关闭文件

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

int main()
{
	int fd;
	fd = open("./file1", O_RDWR);	//打开当前路径下file1文件 
	if(fd != -1){
		printf("打开文件成功"); 
	}
	
	close(fd);	//关闭文件
	
	return 0;
}

注:文件打开后一定要记得关闭,不然会损坏文件.

三、写入文件

ssize_ t write(int fd, const void *buf ,size_ t count);

参数解释:
fd:文件描述符。

buf:数据缓冲区

count:写入文件的大小。

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

int main()
{
	int fd;
	char *buf  = "luo xian yi hen shuai" ;
	
	fd = open("./file1", O_RDWR);	//打开当前路径下file1文件 
	if(fd == -1){
		printf("打开文件失败\n");
		
		fd = open("./file1", O_RDWR | O_CREAT, 0600);	//打开当前路径下file1文件,如果没有就创建,
		if(fd > 0){
			printf("创建文件成功\n"); 
		}
	}
	printf("打开文件成功: fd = %d\n",fd);
	
	write(fd, buf, strlen(buf));	//把内容写入文件
	
	close(fd);	//关闭文件 
	
	return 0;
}

运行结果:
在这里插入图片描述
打开file1文件夹后:
在这里插入图片描述
注:写完文件后,一定要将光标移到头,否则读取不到内容

四、读取文件

ssize_ t read(int fd, void *buf, size_ t count);

参数解释:
fd:文件描述符。

buf:缓冲区

count:读取文件的大小。

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

int main()
{
	int fd;
	int n_write;
	int n_read;
	char *buf  = "luo xian yi hen shuai" ;
	char *readBuf;
	readBuf = (char *)malloc(sizeof(char)*n_write + 1);
	
	fd = open("./file1", O_RDWR);	//打开当前路径下file1文件 
	if(fd == -1){
		printf("打开文件失败\n");
		
		fd = open("./file1", O_RDWR | O_CREAT, 0600);
		if(fd > 0){
			printf("创建文件成功\n"); 
		}
	}
	printf("打开文件成功: fd = %d\n", fd);
	
	n_write = write(fd, buf, strlen(buf));
	if (n_write != -1){
			printf("写入成功\n");
	}
	close(fd)	//关闭文件 
	fd = open("./file1", O_RDWR);	//重新打开文件 
	n_read = read(fd, readBuf, n_write);	//读文件 
	
	printf("读了%d,内容是:%s", n_read, readBuf);
	
	close(fd);	//关闭文件 
	
	return 0;
}

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

注:读取文件之前一定要把光标移到头,或者重新打开
光标移到头:

lseek(fd, -21, SEEK_CUR);

五、光标移动操作

off_t lseek(int fd, off_t offset, int whence);

参数解释:
fd:文件描述符

offset(偏移值):相对于固定点的位置(负数往前走,正数往后)

whence(固定点位置):

  1. SEEK_SET——头
  2. SEEK_END——尾
  3. SEEK_CUR——当前光标位置

使用光标计算文件大小:

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

int main()
{
	int fd;
	int size;
	
	fd = open("./file1", O_RDWR);
	
	size = lseek(fd, 0, SEEK_END);
	printf("文件的大小是:%d\n",size);
	
	close(fd);
	
	
	return 0;
}

**运行结果:**

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值