Tuesday 8 March 2016

WAP for command line arguments


#include<stdio.h>
#include<conio.h>

main(int arg, char *ar[])
{
  int n,i;
  clrscr();
  for(i=0;i<arg;i++)
  printf("argv[%d] = %s\n",i,ar[i]);
  getch();
}



WAP for copy file using command line arguments


#include<stdio.h>
#include<conio.h>

main(int argc, char *argv[])
{
  FILE *source,*dest;
  int c;
  clrscr();
  if(argc!=3)
  {
   printf("\nWrong no of arguments ");
   exit(1);
  }
  if((source=fopen(argv[1],"r"))==NULL)
  {
   printf("\nCan't open file ");
   exit(1);
  }
  if((dest=fopen(argv[2],"w"))==NULL)
  {
   printf("\nCan't open file ");
   exit(1);
  }
  while((c=fgetc(source))!=EOF)
  fputc(c,dest);

  fclose(source);
  fclose(dest);
  printf("\ndone");
  getch();
}


WAP to check how many arguments are supplied


#include <stdio.h>
#include <conio.h>
int main( int argc, char *argv[] )  {

   if( argc == 2 ) {
      printf("\nThe argument supplied are %s\n", argv[1]);
   }
   else if( argc > 2 ) {
      printf("\nToo many arguments have supplied");
   }
   else {
      printf("\nOnly one argument is expected");
   }
   getch();
   return 0;
}


WAP to give basic information


#include <stdio.h> 
#include <conio.h>    
void main(int argc, char *argv[] )  { 
     
printf("My Program Name Is: %s\n", argv[0]); 
      
     if(argc < 2){ 
          printf("\nNo argument are passed"); 
     } 
     else{ 
          printf("First argument: %s\n", argv[1]); 
     } 
getch();
    } 

No comments:

Post a Comment