Thursday, December 25, 2008

Command line argument Question with Answers

Q.What is command prompt?
Ans: Command prompt is text mode interface by which user can enter the command from keyboard. It is non-graphical interface. These commands invoke the specific program .
Q.What is command line argument?
Ans: With help of main function in c we can invoke the program. main function is called by operating system. In main function has the parameter. They are:
1. Argument counter
2. Argument vector
3. Environment vector

It is written as :
main (int argument_counter,char *argument_vector[ ],char *environment_vector[ ])
Argument counter: It is integer type data type. It always count the number of argument written to execute the any command and store that number.
Example:
void main (int argument_counter )
{
printf(“Total number of argument :\t%d”,argument_counter);
}
Save this program .Let file name is count.c and compile and execute the program then
open run in window then write cmd and press enter. Now go to your current working directory (generally it is c:\tc\bin)


Now if you will count command and argument like India,pak etc then out put will be total number of argument including count.
Argument vector: It is array which contain address of char data type i.e address of all argument.
Example:
void main(int argument_counter,char *argument_vector[])
{
int i;
for(i=0;i
{
printf("%s\n",argument_vector[i]);
}
}
Save this program as arg_vect.c. now write the command in command prompt.


It is displaying all the argument including arg_vect.
How to create dos command in c?
Write a c program to create open command which is similar to type command in dos operating system?
Ans:
#include
void main(int count,char * argv[])
{
int i;
FILE *ptr;
char *str;
char ch;
if(count==1)
{
printf("The syntax of the command is incorrect.\n");
}
for(i=1;i
{
ptr=fopen(argv[i],"r");
if(ptr==NULL)
{
printf("The system cannot find the file specified.");
if(count>2)
printf("\nError occurred while procesing : %s.\n",argv[i]);
}
else
{
if(count>2)
{
printf("%s\n\n",argv[i]);
}
while((ch=getc(ptr))!=-1)
printf("%c",ch);
}
fclose(ptr);
}
}
Save the above file as open.c ,compile and execute the go to command mode (current working directory) and write: open India.c
To run the open command in all directories and drive you will have to give the path of current working directory in command mode. write:
C:tc\bin>PATH c:\tc\bin
Now press enter key. Now your open command will work in all directory and drive.
Write a c program to create list command which is similar to dir command in dos operating system?
Ans:
#include
#include
void main(int count,char *argv[])
{
struct find_t q ;
int a;
if(count==1)
argv[1]="*.*";
a = _dos_findfirst(argv[1],1,&q);
if(a==0)
{
while (!a)
{
printf(" %s\n", q.name);
a = _dos_findnext(&q);
}
}
else
{
printf("File not found");
}
}

Save the above file as list.c ,compile and execute the go to command mode (current working directory) and write: list *.c
To run the list command in all directories and drive you will have to give the path of current working directory in command mode. write:
C:tc\bin>PATH c:\tc\bin
Now press enter key. Now your list command will work in all directory and drive.

In same way you can create cls ,copy con etc commands.
Environment vector: It is array which contain address of char data type i.e address of all environment vector.
Example:
void main(int argument_counter,char *argument_vector[],char *enviroment_vector[ ])
{
int i=0;
while(enviroment_vector[i])
{
printf("%s\n",enviroment_vector[i]);
i++;
}
}
Its output will be list of all environment vectors.

No comments:

Blog List