fwrite实现文件复制
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 #include7 #include 8 #include 9 10 #define BUFSIZE 10011 12 int main(int argc, char * argv[])13 {14 FILE *read_fp,*write_fp;15 char buf[BUFSIZE];16 17 if(3 != argc)18 {19 printf("Usage:%s \n",argv[0]);20 exit(1);21 }22 if(NULL == (read_fp = fopen(argv[1],"r")))23 {24 perror("malloc");25 exit(1);26 }27 if(NULL == (write_fp = fopen(argv[2],"w")))28 {29 perror("malloc");30 exit(1);31 }32 33 while(!feof(read_fp) && !ferror(read_fp))34 {35 bzero(buf,BUFSIZE);//在使用前一定要先清缓冲36 fread(buf,BUFSIZE,1,read_fp);//读取文件37 fwrite(buf,strlen(buf),1,write_fp);//写文件,注意要用strlen,因为最后一次可以装不满缓冲区38 }39 40 fclose(read_fp);41 fclose(write_fp);42 43 44 return 0;45 }
fgets实现文件复制
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //copy file by line 7 8 #include9 #include 10 11 #define MAXSIZE 102412 13 int main(int argc, char * argv[])14 {15 FILE *read_fp,*write_fp;16 char buf[MAXSIZE];17 18 if(3 != argc)19 {20 printf("Usage:%s \n",argv[0]);21 exit(1);22 }23 if(NULL == (read_fp = fopen(argv[1],"r")))24 {25 perror("malloc");26 exit(1);27 }28 if(NULL == (write_fp = fopen(argv[2],"w")))29 {30 perror("malloc");31 exit(1);32 }33 34 while(NULL != fgets(buf,MAXSIZE,read_fp))//fgets()浼氭妸'\n'绠楀湪缂撳啿鍖轰腑35 {36 fputs(buf,write_fp);//paste file37 }38 39 fclose(read_fp);40 fclose(write_fp);41 42 return 0;43 }
fgetc实现文件
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //copy file by char 7 8 #include9 #include 10 11 12 int main(int argc, char * argv[])13 {14 FILE *read_fp,*write_fp;15 char tmp;16 17 if(3 != argc)18 {19 printf("Usage:%s \n",argv[0]);20 exit(1);21 }22 if(NULL == (read_fp = fopen(argv[1],"r")))23 {24 perror("malloc");25 exit(1);26 }27 if(NULL == (write_fp = fopen(argv[2],"w")))28 {29 perror("malloc");30 exit(1);31 }32 33 while(EOF !=(tmp= fgetc(read_fp)))//copy file34 {35 fputc(tmp,write_fp);//paste file36 }37 38 fclose(read_fp);39 fclose(write_fp);40 41 42 return 0;43 }