user1232148
user1232148

Reputation: 11

How to pass an argument from a c++ program to cmd?

I am trying to use an input of a user to be used as an argument that will be passed to cmd...

I know that to use cmd from my program I need to use this:

system("SayStatic.exe hello world");

but this is what I need:

char item[100];
gets(item);
//after getting the input I need to pass it to SayStatic.exe that is the part I dont know

I know I cannot use sysytem(); for that but others like spawnl() or execl() would work?

Upvotes: 1

Views: 217

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 994659

First, never use gets(). It should never have been included in the standard library, as input that is longer than the string will overwrite stack memory and cause undefined behaviour (probably a crash of some kind). fgets() is an acceptable substitute, if you're using C strings.

You can do this using C++ strings like this:

std::string line;
std::getline(std::cin, line);
system(("SayStatic.exe " + line).c_str());

Upvotes: 3

Related Questions