CARVIEW |
Select Language
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 24 Jul 2025 19:46:27 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Last-Modified: Wed, 22 May 2024 15:38:28 GMT
Content-Encoding: gzip
how can i use command line in my applica - C++ Articles




for an example, you want to use a feature with just one command
or call a function with one command and some parameters
or you want to get an input from user or you want to get an option that ask's remove the file or not
and many thinks that maybe you need them
you can do them by command line
so, a command is an instruction
commands executed line by line
when you type a command and press enter, it will be executed
for example this is a command:
we call help in this command
but an application may use another command for help or some other commands for do something
you can use them by using Standard C and C++ functions like printf, scanf, etc
this is an example that show's how to add an option to specify the help
lets translate the code
first, we included the header files
after that we use the std namespace
we create the help function for specifying -h
in main function, we have 2 parameters
argc as a type of int
and argv is array pointer to char
notice that we can use char* argv[] instead of char** argv
in the if expression, we use strcmp that take's 2 parameters
boath of them are string
we use argv[0] for accessing first index of the array
at second, we specify the option to be used for help
in the if statement we call help function
and at last, we write help message in the help function
we created one option(file input) that get's an input from user and open's it
in main function:
we created one string called input
we added help message by using "-h"
then in the if statement, we pass input as the second parameter
then, we use fopen() to open that file
argc is the number of options that user specify's
so, argv[argc-1] is the last index of the array
but when argc=1, argv[0] specify's
this is because of the number of parameters that user specify's
and argv[0] is the first index
at last,but not leest, you can limit the number of parameters that user specify's by adding another if statement in main function like this:
as you can see, parameters limited to 4
but you can limit it to another number
- Articles
- how can i use command line in my applica
Published by heepoo
Jul 10, 2013 (last update: Aug 25, 2013)
how can i use command line in my application?
Score: 3.0/5 (97 votes)





what is the command line
sometimes, your application must do somethingfor an example, you want to use a feature with just one command
or call a function with one command and some parameters
or you want to get an input from user or you want to get an option that ask's remove the file or not
and many thinks that maybe you need them
you can do them by command line
intruduction to command line
command line is a window that user type's commands in it and computer does themso, a command is an instruction
commands executed line by line
when you type a command and press enter, it will be executed
for example this is a command:
|
|
we call help in this command
but an application may use another command for help or some other commands for do something
how to use command line in the programs
well, it is so easy!you can use them by using Standard C and C++ functions like printf, scanf, etc
this is an example that show's how to add an option to specify the help
|
|
lets translate the code
first, we included the header files
after that we use the std namespace
we create the help function for specifying -h
in main function, we have 2 parameters
argc as a type of int
and argv is array pointer to char
notice that we can use char* argv[] instead of char** argv
in the if expression, we use strcmp that take's 2 parameters
boath of them are string
we use argv[0] for accessing first index of the array
at second, we specify the option to be used for help
in the if statement we call help function
and at last, we write help message in the help function
how to get an input file from user
this example show's how to get a file from user and open it
|
|
we created one option(file input) that get's an input from user and open's it
in main function:
we created one string called input
we added help message by using "-h"
then in the if statement, we pass input as the second parameter
then, we use fopen() to open that file
notes
you can access other indexes of argvargc is the number of options that user specify's
so, argv[argc-1] is the last index of the array
but when argc=1, argv[0] specify's
this is because of the number of parameters that user specify's
and argv[0] is the first index
at last,but not leest, you can limit the number of parameters that user specify's by adding another if statement in main function like this:
|
|
as you can see, parameters limited to 4
but you can limit it to another number
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.3
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.3
Spotted an error? contact us