C问题

进入大学后.高中接触的C忘的差不多..大学学的语言不少..也许是自己这个人就是混惯了..连编程也是混编.除了JAVA没混编过..C/C++MS 没分过..尤其用的Dev-C++编译器..在ACM一年里简直就是混编过来的.没办法.谁让这个编译器写明是C++.却能包揽C(谁让C++兼容C呢?)..结果让我这个爱混的人就不知道混成啥样了..到现在才发现..自己对C..就知道点点语法了(严重点语法都忘了).现在开Linux 课了.Mr杨不得不让我们重拾C,才知道自己对C..对别的东东见识太短(难道真是头发太长?下次剪头发去..呵呵),这次系主任同志(Mr杨)让我们用纯C实现一个文件或目录的拷贝(全班了汗了一把.还说减轻对我们的要求,不用实现目录嵌套(那东东肯定会用到递归的),下次再搞定.)或是Shell脚本..对脚本.我们可算是基本不会的说..
今天搞完体能测试之后做了下..大致的结果是出来.但还是出现了问题..复制文件和目录是没问题..但它是在复本文件中多了个尾巴(多加了两字母).头痛...应该是出现在复制(即读写文件出现问题了).可是没想透..现把代码帖出来..待高手解答:
Code:
  1. //现若只有两个参数..一个源文件/目录地址,,一个目标文件/目录地址    
  2.  #include <cstdlib>   
  3. #include <iostream>   
  4. #include<stdio.h>   
  5. #include<sys/stat.h>   //文件属性的头文件    
  6. #include <sys/types.h>    
  7. #include<dir.h>   
  8. #include<string>   
  9. using namespace std;   
  10.  FILE *fp1,*fp2;//两个文件指针    
  11.  long i;   
  12.  char dfile[500]="";   
  13.  struct _finddata_t ff;   
  14. void file_copy(FILE *sf,FILE *df){   
  15.     while(!feof(sf)){   
  16.     fputc(fgetc(sf),df);   
  17.     }      
  18.     fclose(sf);   
  19.     fclose(df);     
  20. }   
  21. //处理单个文件   
  22.   void  copy_single(char *sf,char *df){   
  23.            
  24.          if((fp1=fopen(sf,"r"))==NULL){   
  25.               printf("the source file can not open!!");   
  26.               exit(0);   
  27.               }      
  28.                if((fp2=fopen(df,"w"))==NULL){   
  29.               printf("the object file can not open!!");   
  30.               exit(0);                                
  31.               }          
  32.                
  33.               file_copy(fp1,fp2);       
  34.            
  35. }    
  36. void copy_director(char *sf,char *df){   
  37.         
  38.       _chdir(sf); //目录   
  39.       int count=0;   
  40.         if((i=_findfirst("*.*", &ff))==-1L)   
  41.          {   
  42.             printf("空白!/n");   
  43.             exit(0);   
  44.          }   
  45.            while(_findnext(i, &ff)==0)   
  46.            {   
  47.             char sfile[500]="";                
  48.             //  printf("%s/n%s/n", argv[j],ff.name);   
  49.               strcat(sfile,sf);   
  50.               strcat(sfile,"//");   
  51.               strcat(sfile,ff.name);   
  52.               printf("-------%s/n", sfile);   
  53.                  
  54.                  
  55.                if(count++==0) continue;   
  56.               if(df!=NULL) strcpy(dfile,df);   
  57.                  
  58.              // 处理两个文件指针 ..即指向文件   
  59.               if((fp1=fopen(sfile,"r"))==NULL){   
  60.               printf("the source file can not open!!");   
  61.               exit(0);   
  62.               }    
  63.               //处理各目标文件名    
  64.               strcat(dfile,"//");   
  65.               strcat(dfile,ff.name);   
  66.               printf("===========%s/n",dfile);   
  67.                  
  68.               if((fp2=fopen(dfile,"w"))==NULL){   
  69.               printf("the object file can not open!!");   
  70.               exit(0);                                
  71.               }   
  72.               file_copy(fp1,fp2);   
  73.           }//while   
  74.         _findclose(i);   
  75. }   
  76.     
  77. int main(int argc, char *argv[])   
  78. {   
  79.      
  80.   struct stat buf;   
  81.    if(argc<3) exit(0);   
  82.     if(stat(argv[1],&buf)<0)   
  83.         {   
  84.          perror("出错:");   
  85.          exit(0);   
  86.         }       
  87.       if(S_ISREG(buf.st_mode)){   
  88.         //说明这是个普通文件  ..只要处理单个文件即可   
  89.              copy_single(argv[1],argv[2]);       
  90.       }   
  91.       if(S_ISDIR(buf.st_mode)){   
  92.       //说明这是个文件夹     
  93.        copy_director(argv[1],argv[2]);   
  94.       }//fi   
  95.         fclose(fp1);   
  96.         fclose(fp2);   
  97.     system("PAUSE");   
  98.     return 0;   
  99. }   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值