+ 2
How can i pass variables to shell through system() command??
I would like to pass variables to cmd shell using system () but can't figure out how in c/c++
18 ответов
+ 4
Thats easy :
int a; cin>>a
system(("echo \"Your Variable is - "+to_string(a)+"\"").c_str());
Perhaps you don't need the quotes in echo commands, I am not sure though.
+ 3
Consider the simple command explorer, used to open a folder. It requires a single argument, the path of the location to be opened.
Then to create a C++ program that can open any folder for you when you enter a path, you do:
string path;
cout<<"Enter a Path in this PC - ";getline(cin,path);
system(("explorer \"+path+"\"").c_str());
Note the \" used in the command, as there may exist spaces in the string, and the rule says to enclose them in quotes.
Similarly, you may use other commands like cd, shutdown, ls, mkdir, etc and even pass integers etc but you will have to convert them back to a string, using the to_string() function.
+ 3
Not exactly, but yes, you may think of it like that. Its more like a shortcut to get things done like you want them to.
+ 3
Yes. But do you wish to print a C++ variable (int a) or a cmd variable ($1)?
+ 3
to_string is a function used to convert a variable from any type to a string. I used it as it is easier to print the integer as a string.
+ 2
c_str() is a method of the string type used to convert a string back to a character array.
You see, the system command specifically requires a character array (char a[]).
A general example for c_str() is :
string str; getline(cin,str);
char a[20] = str.c_str();
+ 2
The explorer command, or any other cmd command which requires a path, needs the path to be enclosed in double inverted quotes if you are typing a path that has a space in between. Otherwise, it will just read all the path till the space, which you may have not wanted.
Eg -
Suppose you wish to open the folder :
H:\Docs\My Saved Files\
If you type this in cmd, you will not get a correct result:
explorer H:\Docs\My Saved Files
This was as the explorer command is searching for the folder 'My' inside the folder 'Docs', which may not even exist. So the result is something unexpected.
Instead, if you then do :
explorer "H:\Docs\My Saved Files"
You will get a correct result, as now it knows what to search for.
The same applies to cd, ls, mkdir, start and other commands that require a path. start however, has a more bizarre syntax, and requries "" before the path , like this:
start "" "H:\Docs\My Saved Files\a.docx"
Now, in c++, Typing something in inverted quotes makes it a string. But when you type """, The program returns an error as it thinks you started a new string. So, to print the quote as it is, we need to escape it using \. Thus, I type "\"", which is a string containing an inverted quote.
+ 2
thanks a lot man
+ 2
Welcome ^_^
+ 2
@JPM7
It still doesn't. But I assumed that if he is using system, he must be on a PC, as system commands don't work on Code Playground (Most of them don't).
+ 1
what does c_str()mean
+ 1
I didn't get y u used /"
+ 1
so its like telling the compiler to take in the statement as argument forcefully
+ 1
thanks alot
0
lastly can u declare a variable n then echo it
0
c++
0
but using system()
0
to_string?