Linux——open函数、read函数、write函数简单封装使用

问题1:利用读写特性,write写入某个数据之后,将其read读取出来


  • WR.c 源代码
//打开文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//读写,关闭
#include <unistd.h>

//打开文件	
int openFile(const char* filePath);

//读取文件
void readFile(int fd);

//写入数据
int writeFile(int fd, char* str);

//将光标移到起始位置
int backToStart(int fd);

//写入数据并读取数据
int WRFile(const char* filePath, const char* dataSource);

int main(void){
	//写入数据并读取
	WRFile("1.txt", "this is a test data");
	
	return 0;
}

//打开文件	
int openFile(const char* filePath){
	printf("正在打开%s文件····\n\n",filePath);
	int fd = open(filePath, O_RDWR);
	if(fd == -1){
		perror("打开失败!\n\n");
		return -1;
	}
	printf("打开成功 !\n\n");

	return fd;
}


//读取文件
void readFile(int fd){
	char c[2] = {0};
	int flag = 1;
	printf("正在读取····\n\n");
	do{
		flag = read(fd, c, sizeof(c)-1);
		if(flag > 0){
			printf("%s", c);
		}

	}while(flag);
	printf("\n\n读取成功 !\n\n");
}

//写入数据
int writeFile(int fd, char* str){
	printf("正在写入····\n\n");

	printf("%s\n\n",str);
	int w = write(fd, str, strlen(str));
	printf("写入成功 !\n\n");
	return w;
}


//将光标移到起始位置
int backToStart(int fd){
	return lseek(fd, 0, SEEK_SET);
}

//写入数据并读取数据
int WRFile(const char* filePath, const char* dataSource){

	//打开文件
	int fd = openFile(filePath);

	//写入数据
	writeFile(fd,dataSource);
	
	//将光标移到起始位置
	backToStart(fd);
	
	//读取文件到屏幕
	readFile(fd);

	close(fd);
}
  • 编译代码
even@ubuntu:~/Desktop/shared/day05$ ls
1.txt  2.txt  CopyFile.c  WR.c
even@ubuntu:~/Desktop/shared/day05$ gcc WR.c -o main1
  • 测试

even@ubuntu:~/Desktop/shared/day05$ ./main1 
正在打开1.txt文件····

打开成功 !

正在写入····

this is a test data

写入成功 !

正在读取····

this is a test data

读取成功 !

even@ubuntu:~/Desktop/shared/day05$ 

  • 查看 1.txt 文件内容
this is a test data

问题2:实现复制功能,将文件1的数据复制到文件2中


  • CopyFile.c 源代码
//打开文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
//读写,关闭
#include <unistd.h>

//打开文件	
int openFile(const char* filePath);

//得到文件大小
int getFileSize(int fd);

//拷贝文件1到文件2
int copyFile(const char* sourceFile, const char* targetFile);


int main(void){
	//将1.txt文件内容拷贝到2.txt文件
	copyFile("1.txt","2.txt");

	return 0;
}

//打开文件	
int openFile(const char* filePath){
	printf("正在打开%s文件····\n\n",filePath);
	int fd = open(filePath, O_RDWR);
	if(fd == -1){
		perror("打开失败!\n\n");
		return -1;
	}
	printf("打开成功 !\n\n");

	return fd;
}


//得到文件大小
int getFileSize(int fd){
	//得到文件大小
	int size = lseek(fd, 0, SEEK_END);
	lseek(fd, 0, SEEK_SET);
	return size;
}



//拷贝文件1到文件2
int copyFile(const char* sourceFile, const char* targetFile){
	//打开源文件和目标文件
	int sourFD = openFile(sourceFile);
	int targFD = openFile(targetFile);

	//暂存数据
	int fileSize = getFileSize(sourFD) + 1;
	char str[fileSize];

	printf("正在读取数据源··· \n\n");
	//读取数据
	read(sourFD, str, sizeof(str));
	printf("%s\n\n",str);
	printf("读取成功 !\n\n");


	printf("正在写入目标源··· \n\n");
	//写入目标源
	printf("%s\n\n",str);
	write(targFD, str, strlen(str));
	printf("写入成功 !\n\n");

	//关闭
	close(sourFD);
	close(targFD);
}
  • 编译代码
even@ubuntu:~/Desktop/shared/day05$ ls
1.txt  2.txt  CopyFile.c  main1  WR.c
even@ubuntu:~/Desktop/shared/day05$ gcc CopyFile.c -o main2
even@ubuntu:~/Desktop/shared/day05$ 
  • 测试
even@ubuntu:~/Desktop/shared/day05$ ./main2
正在打开1.txt文件····

打开成功 !

正在打开2.txt文件····

打开成功 !

正在读取数据源··· 

this is a test data


读取成功 !

正在写入目标源··· 

this is a test data


写入成功 !

even@ubuntu:~/Desktop/shared/day05$ 
  • 查看 1.txt 文件内容
this is a test data
  • 查看 2.txt 文件内容
this is a test data

参考文档

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值