當前位置:首頁 » 編程語言 » c語言文件復制

c語言文件復制

發布時間: 2024-06-20 19:08:18

A. c語言函數編寫:文件復制

C語言實現一個簡單的文件復制功能,Linux環境下。
思路步驟:(下代碼最重要的邏輯步驟清晰)
第一步:打開源文件(要復制的文件),打開文件的方式以讀的方式就可以了。
Linux C打開文件的庫函數有:int open(const char *pathname,int flags),int open(const char *pathname,mode_t mode),以及 FILE*fopen(const char *path,const char *mode),FILE *fdopen(int fd,const char *mode),這幾個函數,具體的使用方法就查看manual就可以了。
第二步:創建目標文件,所用的函數也是上面那幾個。
第三步:讀取文件。庫函數有:size_t read(int fd,void *buf,size_t count),
size_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream)
第三步:寫入目標文件。用的庫函數:size_t write(int fd,void *buf,size_t count),
size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE*stream)
第四步:關閉文件。庫函數:int fclose(FILE*fp) ,int close(int fd)
思路步驟就是這樣子的了。下面是具體的代碼實現。

#include
#include
#include
#include
#include
#include

int main(int argc,char *argv[])
{
int fd_source_file,fd__file;//用接受int open()函數返回的值
//FILE *fp_source_file,*fp__file;//如果用FILE *fopen()函數的話
int size_read,size_write;

char buf[1024];
char _file_name[50];
//檢查參數的輸入
if(argc<3)
{
printf("usage: ./a.out source_file_path _file_path\n");
exit(1);
}

//復制目標文件名
strcpy(_file_name,argv[2]);

//打開源文件
if( (fd_source_file=open(argv[1],O_RDONLY,00744))<0 )
{
perror("open source file error");
exit(1);
}

//創建目標文件
if( (fd__file=open(argv[1],O_CREAT|O_RDWR)) <0 )
{
perror("create file error");
exit(1);
}

do
{
//讀取文件內容
if( (size_read=read(fd_source_file,buf,1024)) <0 )
{
perror("read source file error");
exit(1);
}

//寫入目標文件
if( (size_write=write(fd__file,buf,sieze_read))<0 )
{
perror("wrire file error");
exit(1);
}

}while(size_read==1024)

return 0;
}

B. C語言編程實現:將一個文件的內容復制到另一個文件。(詳細點的,考試用。)謝謝!

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#ifdefBUFSIZ

#undefBUFSIZ

#defineBUFSIZ4096

#endif

/*

使用格式:mcpy源文件目標文件

且目標文件和源文件不能一樣,否則會清空文件內容,

源文件必須存在,目標文件可存在也可不存在,如果存在,內容會被覆蓋掉。

*/

intmain(intargc,char**argv)

{

charbuf[BUFSIZ];

intmsglen;

if(argc!=3||strcmp(argv[1],argv[2])==0)

/*argc:命令行模式下,輸入的參數數目。

argv:第一個參數的首地址。*/

{

fprintf(stderr,"******************************** ");

fprintf(stderr,"Pleaseusage:%ssource_filedestination_file Andsource__file ",argv[0]);

fprintf(stderr,"******************************** ");

exit(0);

}

FILE*fp_src,*fp_des;

if((fp_src=fopen(argv[1],"r"))==NULL)

/*為空,則打開失敗*/

{

fprintf(stderr,"open%sfailed! ",argv[1]);

exit(1);

}

if((fp_des=fopen(argv[2],"w"))==NULL)

/*為空,則打開或創建失敗*/

{

fprintf(stderr,"open/create%sfailed! ",argv[2]);

exit(2);

}

while(fgets(buf,BUFSIZ,fp_src)!=NULL)

/*從源文件讀,讀失敗或到達文件尾部時,返回NULL*/

{

if(fputs(buf,fp_des)==EOF)

/*寫入目標文件,寫入失敗時,返回EOF;若成功返回寫入的位元組數*/

{

fprintf(stderr,"%sto%sfailed! ",argv[1],argv[2]);

exit(3);

}

}

printf("%sto%ssuccessful! ",argv[1],argv[2]);

return0;

}

C. 怎樣用C語言將文件復制另外的文件夾

有兩種方式可以實現復制:
一、自行編寫函數,實現復制。
演算法流程如下:
1
以讀的方式打開源文件,以寫的方式打開目標文件;
2
每次讀一個位元組,並寫到目標文件中,直到達到文件結尾為止;
3
關閉兩個文件。
二、調用系統命令。
stdlib.h中的system函數,可以執行系統命令行支持的命令。
int
system(char
*cmd);
調用時就是執行cmd中的指令。
1
對於windows,就是執行dos命令,可以調用
system("
/Y
src_file
target_dir");
其中src_file為源文件,而target_dir就是目標文件夾。
2
對於Linux,需要執行shell命令cp,如下
system("cp
src_file
target_dir");

D. 鎬庢牱鐢–璇璦灝嗘枃浠跺嶅埗鍙﹀栫殑鏂囦歡澶

鏈変袱縐嶆柟寮忓彲浠ュ疄鐜板嶅埗錛
涓銆佽嚜琛岀紪鍐欏嚱鏁幫紝瀹炵幇澶嶅埗銆
綆楁硶嫻佺▼濡備笅錛
1 浠ヨ葷殑鏂瑰紡鎵撳紑婧愭枃浠錛屼互鍐欑殑鏂瑰紡鎵撳紑鐩鏍囨枃浠訛紱
2 姣忔¤諱竴涓瀛楄妭錛屽苟鍐欏埌鐩鏍囨枃浠朵腑錛岀洿鍒拌揪鍒鏂囦歡緇撳熬涓烘錛
3 鍏抽棴涓や釜鏂囦歡銆

浜屻佽皟鐢ㄧ郴緇熷懡浠ゃ
stdlib.h涓鐨剆ystem鍑芥暟錛屽彲浠ユ墽琛岀郴緇鍛戒護琛鏀鎸佺殑鍛戒護銆
int system(char *cmd);
璋冪敤鏃跺氨鏄鎵ц宑md涓鐨勬寚浠ゃ
1 瀵逛簬windows,灝辨槸鎵ц宒os鍛戒護錛屽彲浠ヨ皟鐢
system(" /Y src_file target_dir");
鍏朵腑src_file涓烘簮鏂囦歡錛岃宼arget_dir灝辨槸鐩鏍鏂囦歡澶銆
2 瀵逛簬Linux錛岄渶瑕佹墽琛宻hell鍛戒護cp錛屽備笅
system("cp src_file target_dir");

E. 如何用c語言將文件復制到自己想要的文件夾

C語言里的system("");函數可以執行命令行的幾乎所有指令,把命令行輸入的內容作為參數傳入即可。復制文件的話 應該是: 源文件 目的路徑。
例如命令行里的 c:\test.txt d:\text.txt,
也就是C語言里的:system(" c:\test.txt d:\text.txt");
或者這樣
char c[50] = " c:\test.txt d:\text.txt";
system(c);

熱點內容
去哪裡找自己的支付密碼 發布:2024-10-25 14:46:18 瀏覽:417
生產文件夾 發布:2024-10-25 14:46:14 瀏覽:705
windows搭建ftp伺服器埠修改 發布:2024-10-25 14:46:11 瀏覽:322
勞拉與馬ftp 發布:2024-10-25 00:21:16 瀏覽:359
奪寶網站源碼 發布:2024-10-25 00:19:02 瀏覽:454
編程文本編輯器 發布:2024-10-25 00:09:28 瀏覽:972
編程徐帥 發布:2024-10-25 00:03:25 瀏覽:307
手機安卓模擬器如何打開文件 發布:2024-10-25 00:02:55 瀏覽:722
pythonday 發布:2024-10-24 23:55:47 瀏覽:425
g編譯c文件 發布:2024-10-24 23:55:03 瀏覽:294