用Linux_C語言類比CP命令,實現檔案和檔案夾的拷貝,linux_ccp

來源:互聯網
上載者:User

用Linux_C語言類比CP命令,實現檔案和檔案夾的拷貝,linux_ccp
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<dirent.h>//輸出檔案資訊
#include<sys/stat.h>//判斷是否目錄
int is_dir(char* path){//判斷是否是目錄
struct stat st;
stat(path,&st);
if(S_ISDIR(st.st_mode)){
return 1;
}
else{
return 0;
}
}
/*字串處理函數*/
int endwith(char* s,char c){//用於判斷字串結尾是否為“/”
if(s[strlen(s)-1]==c){
return 1;
}
else{
return 0;
}
}
char* str_contact(char* str1,char* str2){//字串串連
char* result;
result=(char*)malloc(strlen(str1)+strlen(str2)+1);//str1的長度+str2的長度+\0;
if(!result){//如果記憶體動態分配失敗
printf("字串串連時,記憶體動態分配失敗\n");
exit(1);
}
strcat(result,str1);
strcat(result,str2);//字串拼接
return result;
}
/*複製函數*/
void copy_file(char* source_path,char *destination_path){//複製檔案
char buffer[1024];
FILE *in,*out;//定義兩個檔案流,分別用於檔案的讀取和寫入int len;
if((in=fopen(source_path,"r"))==NULL){//開啟源檔案的檔案流
printf("源檔案開啟失敗!\n");
exit(1);
}
if((out=fopen(destination_path,"w"))==NULL){//開啟目標檔案的檔案流
printf("目標檔案建立失敗!\n");
exit(1);
}
int len;//len為fread讀到的位元組長
while((len=fread(buffer,1,1024,in))>0){//從源檔案中讀取資料並放到緩衝區中,第二個參數1也可以寫成sizeof(char)
fwrite(buffer,1,len,out);//將緩衝區的資料寫到目標檔案中
}
fclose(out);
fclose(in);
}
void copy_folder(char* source_path,char *destination_path){//複製檔案夾
if(!opendir(destination_path)){
if (mkdir(destination_path,0777))//如果不存在就用mkdir函數來建立
{
   printf("建立檔案夾失敗!");
}
}
char *path;
path=(char*)malloc(512);//相當於其它語言的String path="",純C環境下的字串必須自己管理大小,這裡為path直接申請512的位置的空間,用於目錄的拼接
path=str_contact(path,source_path);//這三句,相當於path=source_path
struct dirent* filename;
DIR* dp=opendir(path);//用DIR指標指向這個檔案夾
while(filename=readdir(dp)){//遍曆DIR指標指向的檔案夾,也就是檔案數組。
memset(path,0,sizeof(path));
path=str_contact(path,source_path);
//如果source_path,destination_path以路徑分隔字元結尾,那麼source_path/,destination_path/直接作路徑即可 
//否則要在source_path,destination_path後面補個路徑分隔字元再加檔案名稱,誰知道你傳遞過來的參數是f:/a還是f:/a/啊?
char *file_source_path;
file_source_path=(char*)malloc(512);
if(!endwith(source_path,'/')){
file_source_path=str_contact(file_source_path,source_path);
file_source_path=str_contact(source_path,"/");
}
else{
file_source_path=str_contact(file_source_path,source_path);
}
char *file_destination_path;
file_destination_path=(char*)malloc(512);
if(!endwith(destination_path,'/')){
file_destination_path=str_contact(file_destination_path,destination_path);
file_destination_path=str_contact(destination_path,"/");
}
else{
file_destination_path=str_contact(file_destination_path,destination_path);
}
//取檔案名稱與當前檔案夾拼接成一個完整的路徑
file_source_path=str_contact(file_source_path,filename->d_name);
file_destination_path=str_contact(file_destination_path,filename->d_name);
if(is_dir(file_source_path)){//如果是目錄
if(!endwith(file_source_path,'.')){//同時並不以.結尾,因為Linux在所有檔案夾都有一個.檔案夾用於串連上一級目錄,必須剔除,否則進行遞迴的話,後果無法相像
copy_folder(file_source_path,file_destination_path);//進行遞迴調用,相當於進入這個檔案夾進行複製~
}
}
else{
copy_file(file_source_path,file_destination_path);//否則按照單一檔案的複製方法進行複製。
printf("複製%s到%s成功!\n",file_source_path,file_destination_path);
}
}
}
/*主函數*/
int main(int argc,char *argv[]){
if(argv[1]==NULL||argv[1]==NULL){
printf("請輸入兩個檔案夾路徑,第一個為源,第二個為目的!\n");
exit(1);
}
char* source_path=argv[1];//取使用者輸入的第一個參數
char* destination_path=argv[2];//取使用者輸入的第二個參數
DIR* source=opendir(source_path);
DIR* destination=opendir(destination_path);
if(!source||!destination){
//對單個檔案進行拷貝
int len;
char buf[1024];
FILE *in,*out;
in=fopen(argv[1],"r+");
out=fopen(argv[2],"w+");
while(len=fread(buf,1,sizeof(buf),in))
{
fwrite(buf,1,len,out);
}
printf("複製%s到%s成功!\n",argv[1],argv[2]);
return 0;
}
copy_folder(source_path,destination_path);//進行檔案夾的拷貝
return 0;


}


到此,程式寫完,下一步就是編譯運行了:

#gcc -o  copy  copy.c

你想把/mnt/yaffs/abc目錄下的所有東西,拷貝到/share目錄下

#./copy  /mnt/yaffs/abc   /share/abc

假設abc下有a.txt 、檔案夾b、d.c;

那麼命令執行完之後,在/share/abc下就有a.txt 、檔案夾b、d.c了,而且檔案夾b下的東西也會被拷貝過來

拷貝單個檔案a.txt

  ./copy  /mnt/yaffs/abc/a.txt     /share/abc/a.txt


1
0
    查看評論

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.