自己寫的unix檔案拷貝指令cp實現函式
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <strings.h>
//功能
//引數:
//返回值:
//作者:
//日期:
int fileCopy(int s_fd, int d_fd)
{
//讀取s_fd
//注意檔案許可權的拷貝
//每呼叫一次函式就是一次開銷,因此要注意在呼叫函式之前判斷是否要進入函式
char buf[128];
char* tmp;
int r_ret, w_ret;
//寫d_fd
bzero(buf,128);//讀取之前要清空一下
while((r_ret = read(s_fd, buf, 128)) != 0)
{
//保證讀取的內容全部寫到檔案中
tmp = buf;
while( (w_ret = write(d_fd, tmp, r_ret)) != -1)
{
tmp += w_ret;
r_ret -= w_ret;
if(r_ret == 0)
{
break;
}
}
bzero(buf,128);//清空一下資料
}
return 0;
}
int main(int argc, char** argv)
{
int s_fd, d_fd;
s_fd = open(argv[1], O_RDONLY);
d_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0664);
if(s_fd == -1 || d_fd == -1)
{
perror("open");
return 1;
}
fileCopy(s_fd,d_fd);
close(s_fd);
close(d_fd);
return 0;
}
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <strings.h>
//功能
//引數:
//返回值:
//作者:
//日期:
int fileCopy(int s_fd, int d_fd)
{
//讀取s_fd
//注意檔案許可權的拷貝
//每呼叫一次函式就是一次開銷,因此要注意在呼叫函式之前判斷是否要進入函式
char buf[128];
char* tmp;
int r_ret, w_ret;
//寫d_fd
bzero(buf,128);//讀取之前要清空一下
while((r_ret = read(s_fd, buf, 128)) != 0)
{
//保證讀取的內容全部寫到檔案中
tmp = buf;
while( (w_ret = write(d_fd, tmp, r_ret)) != -1)
{
tmp += w_ret;
r_ret -= w_ret;
if(r_ret == 0)
{
break;
}
}
bzero(buf,128);//清空一下資料
}
return 0;
}
int main(int argc, char** argv)
{
int s_fd, d_fd;
s_fd = open(argv[1], O_RDONLY);
d_fd = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0664);
if(s_fd == -1 || d_fd == -1)
{
perror("open");
return 1;
}
fileCopy(s_fd,d_fd);
close(s_fd);
close(d_fd);
return 0;
}
相關文章
- 使用expect指令碼SCP拷貝檔案指令碼
- Java實現檔案拷貝的4種方法.Java
- C++拷貝建構函式(深拷貝,淺拷貝)C++函式
- 淺拷貝與深拷貝的實現
- 拷貝建構函式函式
- C語言實現字串拷貝函式的幾種方法C語言字串函式
- 使用shell指令碼及asm cp或RMAN copy批量將資料檔案從ASM拷貝到檔案系統指令碼ASM
- IOCP 檔案拷貝
- unix批量拷貝方法
- 實現物件淺拷貝、深拷貝物件
- 拷貝建構函式的作用函式
- 深拷貝與淺拷貝的實現(一)
- Linux使用expect實現遠端拷貝檔案Linux
- 使用splice函式實現0拷貝的回顯伺服器函式伺服器
- js實現深拷貝和淺拷貝JS
- 【JS】深拷貝與淺拷貝,實現深拷貝的幾種方法JS
- IO流-檔案拷貝
- 檔案內容拷貝
- Java-0024-用I/O實現拷貝檔案Java
- 拷貝建構函式中的陷阱函式
- 深究cp拷貝資料夾時`/`的用法
- xcopy 實現批處理拷貝檔案或資料夾
- 拷貝建構函式(比較全的)函式
- Golang命令列拷貝檔案Golang命令列
- 跨網路拷貝檔案的簡單實踐
- asm拷貝檔案到檔案系統ASM
- 深拷貝和淺拷貝的區別是什麼?實現一個深拷貝
- [JS系列二]談談深拷貝和淺拷貝,如何實現深拷貝JS
- js實現深拷貝JS
- Android開發中拷貝db檔案寫入SQLiteAndroidSQLite
- Rman的映象拷貝指令碼指令碼
- JavaScript實現淺拷貝的方法JavaScript
- 伺服器程式設計——函式splice實現零拷貝使用解釋伺服器程式設計函式
- [java IO流]之檔案拷貝Java
- c語言拷貝檔案程式C語言
- Python基礎 - 檔案拷貝Python
- 二進位制檔案拷貝
- JS中的深淺拷貝以及實現深拷貝的幾種方法.JS