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

參考文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章