How to take inputs argument separated by commas in C command line? [closed]
up vote
-3
down vote
favorite
(C Program) I need help on an assignment where I must take an input from the command line in the format cmd1,cmd2,cmd3,cmd4
, where each command is separated by a comma. How can I accomplish this? Is there a way where I can the entire line as a string and then parse it?
To explain further, the user executes the program like this after compiling:
./a.out cmd1,cmd2,cmd3,…,cmdN
The number of commands not limited.
If there is a way to take the argument as a single string that would be best, as I can just parse the commands in the program.
I don't need help parsing the text; just some direction on how to get the command line argument.
c command-line
closed as too broad by Jonathan Leffler, chux, Billal Begueradj, qrdl, Rob Nov 11 at 14:45
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-3
down vote
favorite
(C Program) I need help on an assignment where I must take an input from the command line in the format cmd1,cmd2,cmd3,cmd4
, where each command is separated by a comma. How can I accomplish this? Is there a way where I can the entire line as a string and then parse it?
To explain further, the user executes the program like this after compiling:
./a.out cmd1,cmd2,cmd3,…,cmdN
The number of commands not limited.
If there is a way to take the argument as a single string that would be best, as I can just parse the commands in the program.
I don't need help parsing the text; just some direction on how to get the command line argument.
c command-line
closed as too broad by Jonathan Leffler, chux, Billal Begueradj, qrdl, Rob Nov 11 at 14:45
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
I don't need help parsing the text, just some direction on how to get the cmd line argument.
Its inargv
, usually.
– tkausl
Nov 11 at 1:29
1
int main(int argc, char **argv) if (argc != 2) err_usage("%s cmd1,cmd2,…,cmd3", argv[0]); …parse argv[1]…
.
– Jonathan Leffler
Nov 11 at 2:18
@wes sheck Just out of curiorsity: Why don't you use ```(space) to seperate the commands? In that way you can use
argc``` to get the number of commands and useargv
to access them.
– JiaHao Xu
Nov 11 at 2:23
@JiaHaoXu: I think the reason is "an assignment … must take … formatcmd1,cmd2,cmd3,cmd4
" — in other words, the teacher is trying to teach how to do this unorthodox usage for whatever reason, rather than because wes sheck thinks it's a good idea.
– Jonathan Leffler
Nov 11 at 2:53
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
(C Program) I need help on an assignment where I must take an input from the command line in the format cmd1,cmd2,cmd3,cmd4
, where each command is separated by a comma. How can I accomplish this? Is there a way where I can the entire line as a string and then parse it?
To explain further, the user executes the program like this after compiling:
./a.out cmd1,cmd2,cmd3,…,cmdN
The number of commands not limited.
If there is a way to take the argument as a single string that would be best, as I can just parse the commands in the program.
I don't need help parsing the text; just some direction on how to get the command line argument.
c command-line
(C Program) I need help on an assignment where I must take an input from the command line in the format cmd1,cmd2,cmd3,cmd4
, where each command is separated by a comma. How can I accomplish this? Is there a way where I can the entire line as a string and then parse it?
To explain further, the user executes the program like this after compiling:
./a.out cmd1,cmd2,cmd3,…,cmdN
The number of commands not limited.
If there is a way to take the argument as a single string that would be best, as I can just parse the commands in the program.
I don't need help parsing the text; just some direction on how to get the command line argument.
c command-line
c command-line
edited Nov 11 at 2:16
Jonathan Leffler
555k886611013
555k886611013
asked Nov 11 at 1:28
wes sheck
1
1
closed as too broad by Jonathan Leffler, chux, Billal Begueradj, qrdl, Rob Nov 11 at 14:45
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as too broad by Jonathan Leffler, chux, Billal Begueradj, qrdl, Rob Nov 11 at 14:45
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
3
I don't need help parsing the text, just some direction on how to get the cmd line argument.
Its inargv
, usually.
– tkausl
Nov 11 at 1:29
1
int main(int argc, char **argv) if (argc != 2) err_usage("%s cmd1,cmd2,…,cmd3", argv[0]); …parse argv[1]…
.
– Jonathan Leffler
Nov 11 at 2:18
@wes sheck Just out of curiorsity: Why don't you use ```(space) to seperate the commands? In that way you can use
argc``` to get the number of commands and useargv
to access them.
– JiaHao Xu
Nov 11 at 2:23
@JiaHaoXu: I think the reason is "an assignment … must take … formatcmd1,cmd2,cmd3,cmd4
" — in other words, the teacher is trying to teach how to do this unorthodox usage for whatever reason, rather than because wes sheck thinks it's a good idea.
– Jonathan Leffler
Nov 11 at 2:53
add a comment |
3
I don't need help parsing the text, just some direction on how to get the cmd line argument.
Its inargv
, usually.
– tkausl
Nov 11 at 1:29
1
int main(int argc, char **argv) if (argc != 2) err_usage("%s cmd1,cmd2,…,cmd3", argv[0]); …parse argv[1]…
.
– Jonathan Leffler
Nov 11 at 2:18
@wes sheck Just out of curiorsity: Why don't you use ```(space) to seperate the commands? In that way you can use
argc``` to get the number of commands and useargv
to access them.
– JiaHao Xu
Nov 11 at 2:23
@JiaHaoXu: I think the reason is "an assignment … must take … formatcmd1,cmd2,cmd3,cmd4
" — in other words, the teacher is trying to teach how to do this unorthodox usage for whatever reason, rather than because wes sheck thinks it's a good idea.
– Jonathan Leffler
Nov 11 at 2:53
3
3
I don't need help parsing the text, just some direction on how to get the cmd line argument.
Its in argv
, usually.– tkausl
Nov 11 at 1:29
I don't need help parsing the text, just some direction on how to get the cmd line argument.
Its in argv
, usually.– tkausl
Nov 11 at 1:29
1
1
int main(int argc, char **argv) if (argc != 2) err_usage("%s cmd1,cmd2,…,cmd3", argv[0]); …parse argv[1]…
.– Jonathan Leffler
Nov 11 at 2:18
int main(int argc, char **argv) if (argc != 2) err_usage("%s cmd1,cmd2,…,cmd3", argv[0]); …parse argv[1]…
.– Jonathan Leffler
Nov 11 at 2:18
@wes sheck Just out of curiorsity: Why don't you use ```
(space) to seperate the commands? In that way you can use
argc``` to get the number of commands and use argv
to access them.– JiaHao Xu
Nov 11 at 2:23
@wes sheck Just out of curiorsity: Why don't you use ```
(space) to seperate the commands? In that way you can use
argc``` to get the number of commands and use argv
to access them.– JiaHao Xu
Nov 11 at 2:23
@JiaHaoXu: I think the reason is "an assignment … must take … format
cmd1,cmd2,cmd3,cmd4
" — in other words, the teacher is trying to teach how to do this unorthodox usage for whatever reason, rather than because wes sheck thinks it's a good idea.– Jonathan Leffler
Nov 11 at 2:53
@JiaHaoXu: I think the reason is "an assignment … must take … format
cmd1,cmd2,cmd3,cmd4
" — in other words, the teacher is trying to teach how to do this unorthodox usage for whatever reason, rather than because wes sheck thinks it's a good idea.– Jonathan Leffler
Nov 11 at 2:53
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
When you run a c
program, the main
function gets 2 parameters:
main(int argc, char *argv)
where the first one is the number of the command line arguments, the second is the array which contains the arguments.
command line arguments are split by spaces and parts of the string constitute the argv
array.
In your case 'cmd1,cmd2,cmd3' does not seem to contain any space, so these will be a single element in the argv
array. As soon as you add spaces, you will get more args. i.e. cmd1 , cmd2
will end up in 3 args: cmd1
, ,
and cmd2
.
I guess your task would be to merge all args into a single string and then parse this string, tokenizing it by the ,
delimiter.
Note that argv[0] will contain the path of the program you run.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
When you run a c
program, the main
function gets 2 parameters:
main(int argc, char *argv)
where the first one is the number of the command line arguments, the second is the array which contains the arguments.
command line arguments are split by spaces and parts of the string constitute the argv
array.
In your case 'cmd1,cmd2,cmd3' does not seem to contain any space, so these will be a single element in the argv
array. As soon as you add spaces, you will get more args. i.e. cmd1 , cmd2
will end up in 3 args: cmd1
, ,
and cmd2
.
I guess your task would be to merge all args into a single string and then parse this string, tokenizing it by the ,
delimiter.
Note that argv[0] will contain the path of the program you run.
add a comment |
up vote
1
down vote
When you run a c
program, the main
function gets 2 parameters:
main(int argc, char *argv)
where the first one is the number of the command line arguments, the second is the array which contains the arguments.
command line arguments are split by spaces and parts of the string constitute the argv
array.
In your case 'cmd1,cmd2,cmd3' does not seem to contain any space, so these will be a single element in the argv
array. As soon as you add spaces, you will get more args. i.e. cmd1 , cmd2
will end up in 3 args: cmd1
, ,
and cmd2
.
I guess your task would be to merge all args into a single string and then parse this string, tokenizing it by the ,
delimiter.
Note that argv[0] will contain the path of the program you run.
add a comment |
up vote
1
down vote
up vote
1
down vote
When you run a c
program, the main
function gets 2 parameters:
main(int argc, char *argv)
where the first one is the number of the command line arguments, the second is the array which contains the arguments.
command line arguments are split by spaces and parts of the string constitute the argv
array.
In your case 'cmd1,cmd2,cmd3' does not seem to contain any space, so these will be a single element in the argv
array. As soon as you add spaces, you will get more args. i.e. cmd1 , cmd2
will end up in 3 args: cmd1
, ,
and cmd2
.
I guess your task would be to merge all args into a single string and then parse this string, tokenizing it by the ,
delimiter.
Note that argv[0] will contain the path of the program you run.
When you run a c
program, the main
function gets 2 parameters:
main(int argc, char *argv)
where the first one is the number of the command line arguments, the second is the array which contains the arguments.
command line arguments are split by spaces and parts of the string constitute the argv
array.
In your case 'cmd1,cmd2,cmd3' does not seem to contain any space, so these will be a single element in the argv
array. As soon as you add spaces, you will get more args. i.e. cmd1 , cmd2
will end up in 3 args: cmd1
, ,
and cmd2
.
I guess your task would be to merge all args into a single string and then parse this string, tokenizing it by the ,
delimiter.
Note that argv[0] will contain the path of the program you run.
answered Nov 11 at 3:23
Serge
3,2782912
3,2782912
add a comment |
add a comment |
3
I don't need help parsing the text, just some direction on how to get the cmd line argument.
Its inargv
, usually.– tkausl
Nov 11 at 1:29
1
int main(int argc, char **argv) if (argc != 2) err_usage("%s cmd1,cmd2,…,cmd3", argv[0]); …parse argv[1]…
.– Jonathan Leffler
Nov 11 at 2:18
@wes sheck Just out of curiorsity: Why don't you use ```
(space) to seperate the commands? In that way you can use
argc``` to get the number of commands and useargv
to access them.– JiaHao Xu
Nov 11 at 2:23
@JiaHaoXu: I think the reason is "an assignment … must take … format
cmd1,cmd2,cmd3,cmd4
" — in other words, the teacher is trying to teach how to do this unorthodox usage for whatever reason, rather than because wes sheck thinks it's a good idea.– Jonathan Leffler
Nov 11 at 2:53