+ 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++

18th Nov 2017, 2:56 AM
blacksheep
blacksheep - avatar
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.
18th Nov 2017, 11:57 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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.
18th Nov 2017, 3:26 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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.
18th Nov 2017, 11:46 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
Yes. But do you wish to print a C++ variable (int a) or a cmd variable ($1)?
18th Nov 2017, 11:52 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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.
18th Nov 2017, 12:02 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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();
18th Nov 2017, 3:37 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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.
18th Nov 2017, 7:45 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 2
thanks a lot man
18th Nov 2017, 11:49 AM
blacksheep
blacksheep - avatar
+ 2
Welcome ^_^
18th Nov 2017, 11:49 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 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).
18th Nov 2017, 5:27 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
what does c_str()mean
18th Nov 2017, 3:30 AM
blacksheep
blacksheep - avatar
+ 1
I didn't get y u used /"
18th Nov 2017, 5:27 AM
blacksheep
blacksheep - avatar
+ 1
so its like telling the compiler to take in the statement as argument forcefully
18th Nov 2017, 9:41 AM
blacksheep
blacksheep - avatar
+ 1
thanks alot
18th Nov 2017, 12:05 PM
blacksheep
blacksheep - avatar
0
lastly can u declare a variable n then echo it
18th Nov 2017, 11:49 AM
blacksheep
blacksheep - avatar
0
c++
18th Nov 2017, 11:53 AM
blacksheep
blacksheep - avatar
0
but using system()
18th Nov 2017, 11:53 AM
blacksheep
blacksheep - avatar
0
to_string?
18th Nov 2017, 11:59 AM
blacksheep
blacksheep - avatar