Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32268
  • 博文数量: 5
  • 博客积分: 45
  • 博客等级: 民兵
  • 技术积分: 101
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-02 20:25
文章分类
文章存档

2014年(3)

2013年(1)

2012年(1)

我的朋友

分类: C/C++

2014-01-23 16:30:49

C语言 文件复制


点击(此处)折叠或打开

  1. #include <sys/types.h>
  2. #include <signal.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <sys/stat.h>

  8. #define BLKSIZE 255
  9. int main(int argc, char *argv[])
  10. {
  11.     
  12.     int fdr,fdw;
  13.     int r_size;
  14.     int iFlen;
  15.     int iBlock = 0;
  16.     char temp[255];
  17.     char filepath[255];
  18.     char fileto[255];
  19.     
  20.     struct stat stbuf;
  21.     
  22.     printf("please input the filepath:");
  23.     
  24.     //    scanf("%s",filepath);
  25.     strcpy(filepath, argv[1]);
  26.     //    fdr = open(argv[1], O_RDONLY|O_BINARY, S_IREAD);
  27. //    fdr = _open(argv[1], O_BINARY);
  28.     fdr = _open(argv[1], O_RDONLY | O_BINARY, S_IWRITE | S_IREAD);
  29.     //    if((fdr=open(filepath,O_RDONLY))==-1)
  30.     if( fdr==-1 )
  31.     {
  32.         printf("file not found,open failed!");
  33.         return -1;
  34.     }
  35.     else
  36.     {
  37.         
  38.         //        iFlen = lseek( fdr, 0L, SEEK_END );
  39.         stbuf.st_size = 0 ;
  40.         stat(filepath,&stbuf);
  41.         iFlen = stbuf.st_size;
  42.         iBlock = BLKSIZE;
  43.         fdw = _open(argv[2], O_WRONLY|O_CREAT|O_BINARY , 0777);
  44.         //        fdw = open(argv[2], O_RDONLY|O_BINARY, S_IREAD);
  45.         for(;;)
  46.         {
  47.             if(iFlen >BLKSIZE)
  48.                 iBlock = BLKSIZE;
  49.             else
  50.                 iBlock = iFlen ;
  51.             memset(temp, 0x00, sizeof(temp));
  52.             r_size=_read(fdr,temp,iBlock);
  53.             if(r_size != iBlock)
  54.             {
  55.                 printf("读数据失败!!!!!!!!!!!![%d, %s]\n", errno, strerror(errno));
  56.                 printf("%d, %d\n", r_size , iBlock);
  57.                 close(fdr);
  58.                 break;
  59.             }
  60.             _write(fdw, temp,iBlock);
  61.             iFlen -=BLKSIZE;
  62.             if(iFlen <= 0 )
  63.             {
  64.                 printf("完成读取文件!!!!\n");
  65.                 break;
  66.             }    
  67.         }
  68.         close(fdw);
  69.         close(fdr);
  70.         printf("\n");
  71.         printf("uuuuuuuuuu,[%d,%s\n", iFlen, temp);
  72.         /*
  73.         while(r_size=read(fdr,temp,255) > 0)
  74.         {
  75.         printf("uuuuuuuuuu,%s\n", temp);
  76.         }
  77.         close(fdr);
  78.         sprintf(fileto,"file-rw.txt");
  79.         if((fdw=open(fileto,O_WRONLY|O_CREAT,0640))==-1)
  80.         {
  81.         printf("open failed!");
  82.         return -1;
  83.         }
  84.         else
  85.         {
  86.         int w_size=write(fdw,temp,r_size);//相当于复制了所输入文件.txt中的255个大小的字符串到新文件file-rw.txt中
  87.         close(fdw);
  88.         printf("have successfully copied 255 chars from \"%s\" to \"%s\"\n",filepath,fileto);
  89.         }
  90.         */
  91.         
  92.     }
  93.     
  94.     return 0;
  95.     
  96. }


阅读(2003) | 评论(0) | 转发(0) |
0

上一篇:openssl 文件加密解密

下一篇:拆线

给主人留下些什么吧!~~