Monday 21 January 2013

A easy copy cmd I code

//copy.c

#include <stdio.h>
#include <stdlib.h>
int main(int argc,char * argv[]){
        FILE *in, *out;
        int c;
        if(argc!=3){
                printf("U forgot to enter a filename!");
        exit(0);
        }
        if((in=fopen(argv[1],"r"))==NULL){
                printf("cannot open infile\n");
                exit(0);
        }
        if((out=fopen(argv[2],"w"))==NULL){
                printf("cannot open outfile\n");
                exit(0);
        }

        while( ( c = fgetc(in) ) != EOF )
        {
                fputc(c, out);
        }

        fclose(in);
        fclose(out);
}






-------------------------------------------------------------------------------------------------------------------------

cc copy.c -o copy
copy fileA fileB

and you get a new fileB same as fileA.

PS: In linux, if you copy a executable file using this program,to run the new file u need to give the new file execute permission(chmod)......

No comments:

Post a Comment