C Problem _c

Source: Internet
Author: User
After entering the university, the high school contacts of C forget almost. There are many languages in college. Maybe he is the one who is accustomed to it. Even programming is mixed. Except for Java. C/c++ms not been divided. Especially with the dev-c++ compiler. In the ACM a year is just mixed up. No way. Who let this compiler specify C + +. But can do C (who make C + + compatible C?). As a result, I don't know how to mix. It's only now that. oneself to C.. I know a little bit of grammar (the severity of the grammar is forgotten). Now I have a Linux class. Mr Yang had to get us back to C before we knew we were on C. Too short for the other stuff (is it really too long?) next time, get your hair cut. hehe), the dean of the Department (Mr Yang) let us use pure C to achieve a copy of a file or directory (the class has a sweat. Also said to reduce our requirements, do not implement directory nesting (that Dongdong will certainly use recursion), the next time to fix. or shell script. To the script. We're basically not going to say that. After the physical test today, I did it. The general result is coming out. But there are still problems. Copying files and directories is no problem. But it had a tail in the copy file (two more letters added). Headache ... It should be in the copy (that is, there is a problem with the read and write file). But I didn't think it through. Now put the code to post it. To master answer: Code://Now if there are only two parameters. A source file/directory address, a target file/directory address        #include  <cstdlib>    #include  < iostream>    #include <stdio.h>    #include <sys/stat.h>   // File properties header file      #include  <sys/types.h>     #include <dir.h>    #include <string>    using namespace std;     file *fp1,*fp2;//two file pointers       long i;    char dfile[500]= "";     struct _finddata_t ff;    void file_copy (FILE&NBSP;*SF,FILE&NBSP;*DF) {       while!feof ( SF)) {   &NBSP;&NBSP;&NBSP;&NBSP;FPUTC (fgetc (SF), DF);        }            fclose (SF);        fclose (DF);     }   //Processing single file       void  copy_single (CHAR&NBSP;*SF,CHAR&NBSP;*DF) {                         if (Fp1=fopen (SF, "r") = = NULL) {                 printf (" the source file can not open!! ");                  exit (0);                  }                       if (Fp2=fopen ( DF, "W")) (==null) {                  printf ("the object file can not open!!");                  exit (0);                                                  }                                         &nbsP; file_copy (FP1,FP2);                    }     void copy_director (char *sf,char *df) {                   _chdir (SF);  //directory          int count=0;            if ((I=_findfirst ("*.*",  &ff)) ==-1L)              {                printf ("Blank!/n");                exit (0);             }                while (_findnext (I,&NBSP;&AMP;FF) ==0)                 {               char  sfile[500]= "";                             //  printf ("%s/n%s/n",  Argv[j],ff.name);                  strcat (Sfile, SF);                  strcat (Sfile, " //");                  strcat (Sfile, Ff.name);                  printf ("------ -%s/n ",  sfile);                                                        if (count++==0)  continue;                  if (Df!=NULL)  strcpy (DFILE,DF);                                   //  processing two file pointer   Point to File                  if (fp1= fopen (Sfile, "R")) ==null) {                  printf ("the source file can not open!!");                  exit (0);                  }                    //process each target filename                     strcat (Dfile, "//");                  strcat (Dfile, Ff.name);                  printf ("= = =====%s/n ", Dfile);                                    if ((Fp2=fopen (Dfile, "w")) = =null) {                 printf ( "the object file can not open!!");                  exit (0);                                                 }                   file_copy (FP1,FP2);              }//while             _findclose (i);   }         int main (int argc, char *argv[])    {& nbsp;          struct stat buf;       if (argc<3)  exit (0);        if (Stat (ARGV[1],&AMP;BUF) <0)             {            p error ("Error:");             exit (0);            }               if (S_isreg (Buf.st_mode)) {           / /Description This is an ordinary document &NBSP;&NBSP;                 copy_ as long as a single file is processed Single (argv[1],argv[2]);              }          if (S_isdir (Buf.st_mode)) {         / /Description This is a folder              copy_director (argv[1],argv[2]);          }//fi             fclose (FP1);            fclose (FP2);    &NBSp;   system ("PAUSE");        return 0;   }   

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.