Command Line Example In C

prog1.c

void main(int argc,char *argv[])
{int i;
printf("%d",argc);
for (i=1;i<argc;i++)
printf("%s\n",argv[i]);
}
c:\prog1.exe 1  4 5 6 6 56 7

showfile.c

#include<process.h>
#include<stdio.h>
void main(int argc, char *argv[])
{ char ch;
FILE *src;
src=fopen(argv[1],"r");
do{
ch=fgetc(src);
if(ch==EOF)
break;
putchar(ch);
}while(1);
}
c:\showfile.exe filename.txt

list.c

#include<process.h>
void main(int argc, char *argv[])
{ char com[20];
if(argc==1)
system("dir");
else
if(argc==2)
{
strcpy(com,"dir ");
strcat(com,argv[1]);
system(com );
}
}
c:\list.exe foldername

copyfile.c

#include<process.h>
#include<stdio.h>
void main(int argc, char *argv[])
{ char ch;
FILE *src,*trgt;
src=fopen(argv[1],"r");
trgt=fopen(argv[2],"w");
do{
ch=fgetc(src);
if(ch==EOF)
break;
fputc(ch,trgt);
}while(1);
}
c:\filecopy.exe file1.txt newfile.txt

Leave a Reply