Total Pageviews

PRASO's Solution for EMBETRONIX & ROBOTIX

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

Sunday, February 27, 2011

Copy the contents of source file to TARGET file using command line argument in C

#include"stdio.h"
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)
    {       
        printf("Target File not Found\n");
        printf("Do you want to create it (Y/N):\n");
        scanf(" %c",&ch);
        if(ch=='y' || ch=='Y')
        {
            t_ptr = fopen(argv[2],"w");
            while((c=fgetc(s_ptr))!=EOF)       
                    fputc(c,t_ptr);
        }
        else
            return;
    }
fclose(s_ptr);
fclose(t_ptr);
}

1 comment:

Anonymous said...

NIce One.....