Total Pageviews

PRASO's Solution for EMBETRONIX & ROBOTIX

SOLUTION PROVIDER OF ANY SORT OF PROBLEMS RELATED TO ELECTRONICS AND TELECOMMUNICATION

Sunday, February 27, 2011

Renaming a File Using command Line Argument in 'C'

#include
main(int argc, char **argv)
{
FILE *s_ptr , *t_ptr;
char ch,c;

    if(argc!=3)
    {
        printf("ERROR: Invalid no. of Arguments\n");
        printf("Usage: copy \n");
        return;
    }

s_ptr = fopen(argv[1],"r");
   
    if(s_ptr==NULL)
    {       
        printf("Source File not Found\n");
        return;
    }

t_ptr = fopen(argv[2],"r+");
   
    if(t_ptr==NULL)
    {       
            t_ptr = fopen(argv[2],"w");
            while((c=fgetc(s_ptr))!=EOF)       
                    fputc(c,t_ptr);
    }
       
fclose(s_ptr);
fclose(t_ptr);\   
remove(argv[1]);
}

No comments: