Tuesday, May 19, 2009

Command Line Argument

/* Title: Command Line Argument
Problem Statement:
Write a program to stimulate copycon, copy & type command using command line argument.


*/

#include
#include
#include

char ch;
FILE *FP1, *FP2;
void main(int argc, char *argv[])
{
clrscr();

if(argc!=3)
{
printf("\nWrong no. of arguments.\n");
exit(1);
}

// Copycon

printf("\nEnter data & Enter $ to terminate:");
FP1=fopen(argv[1],"w");
scanf("%c", &ch);
while(ch != '$')
{
putc(ch, FP1);
scanf("%c", &ch);
}
fclose(FP1);
printf("\n%s File contains:",argv[1]);
FP1=fopen(argv[1],"r");
while( ! feof(FP1))
{
ch=getc(FP1);
printf("%c", ch);
}

printf("\n%s File contains:",argv[2]);
FP2=fopen(argv[2],"r");
while( ! feof(FP2))
{
ch=getc(FP2);
printf("%c", ch);
}
printf("\n\n\n copied file is ");

//Copy
FP1=fopen(argv[1],"r");
if(FP1 == NULL)
{
printf("File not opened");
exit(1);
}

FP2=fopen(argv[2],"w");
if(FP2 == NULL)
{
printf("File not opened");
exit(1);
}


while(!feof(FP1))
{
ch = getc(FP1);
putc(ch,FP2);
}

fcloseall();

// Type
printf("\n\n%s File contains:",argv[2]);
FP2=fopen(argv[2],"r");
while( ! feof(FP2))
{
ch=getc(FP2);
printf("%c", ch);
}

getch();
}

No comments:

Post a Comment