c command line arguments example

c command line arguments example

c command line arguments example

c command line arguments example is complete ready program to start leraning about how to use c command line arguments.

//Written by Adarsh Patel

/*
It shows number of arguments stored in command argument program run
without parameter & with parameter
*/
#include <stdio.h>
#include <conio.h>

void main(int argc, char *argv[])
{
clrscr();
int i;
printf(“\nNumber of Arguments are : %d”,argc);
printf(“\n\n\t\tArgument Text are as follow : “);

for(i=0;i<argc;i++)
{
printf(“\nValues of [%02d] : %s “,i,argv[i]);
//        puts(argv[i]);
}

getch();
}

//Written by Adarsh Patel

/*
This program copy the alphabet from one file to another file with the
use of command line argument
*/
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main(int argc, char *argv[])
{
clrscr();
int i;
char ch1;
printf(“\nNumber of Arguments are : %d”,argc);
printf(“\n\n\t\tArgument Text are as follow : “);

for(i=0;i<argc;i++)
{
printf(“\nValues of [%d] : “,i);
puts(argv[i]);
}
FILE *fp1,*fp2;

fp1 = fopen(argv[1],”w”);

for(i=65;i<=122;i++)
{
if(i>90 && i<97)
continue;
else
fprintf(fp1,”%5c”,i);
}

fclose(fp1);

fp1 = fopen(argv[1],”r”);
fp2 = fopen(argv[2],”w”);

while( (ch1=fgetc(fp1))!= EOF )
{
fprintf(fp2,”%c”,ch1);
}
fclose(fp1);
fclose(fp2);

printf(“\nFile %s content is as follow : “,argv[1]);
fp1 = fopen(argv[1],”r”);
rewind(fp1);
while( (ch1=fgetc(fp1))!= EOF )
{
fprintf(stdout,”%c”,ch1);
}
fclose(fp1);
printf(“\n\n”);

printf(“\nFile %s content is as follow : “,argv[2]);
fp1 = fopen(argv[2],”r”);
rewind(fp1);
while( (ch1=fgetc(fp1))!= EOF )
{
fprintf(stdout,”%c”,ch1);
}
fclose(fp1);

getch();
}

//Written by Adarsh Patel

/*   Use command line arguments to concate two files simultaneously in one file
pass three arguments(total 0 to 3 or 1 to 4)        */
#include <stdio.h>
#include <conio.h>
void main(int argc, char *argv[])
{
clrscr();
int i;
char ch1,ch2;
FILE *fp1,*fp2,*fp3;

printf(“\nNumber of Arguments are : %d”,argc);
printf(“\n\n\t\tArgument Text are as follow : “);

for(i=0;i<argc;i++)
{
printf(“\nValues of [%d] : “,i);
puts(argv[i]);
}

fp1 = fopen(argv[1],”w”);
fp2 = fopen(argv[2],”w”);

for(i=65;i<=122;i++)
{
if(i>90 && i<97)
continue;
else if(i<91)
fprintf(fp1,”%3c”,i);
else
fprintf(fp2,”%3c”,i);
}
fclose(fp1);
fclose(fp2);

fp1 = fopen(argv[1],”r”);    //file 1 for reading
fp2 = fopen(argv[2],”r”);     //file 2 for reading
fp3 = fopen(argv[3],”w”);     //file 3 for concatenate file-1 & 2

do
{
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
fprintf(fp3,”%3c %3c”,ch1,ch2);
//        fprintf(stdout,”%3c %3c”,ch1,ch2);
}while(!feof(fp1));
fclose(fp1);
fclose(fp2);
fclose(fp3);

fp1 = fopen(argv[3],”r”);
printf(“\n%s file content is as follow : “,argv[3]);

while( (ch1=fgetc(fp1))!= EOF )
printf(“\t%c”,ch1);

fclose(fp1);
getch();
}

c programming tutorials

java programming tutorial

android tutorial

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative