pass argument, concatenate string 
June 18th, 2009, 11:35 PM
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,075
| |
All i want to do in C++ is pass a few arguments, concatenate them into variable and call system()
I've gotten this far: -
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string>
-
-
using namespace std;
-
-
int main(int argc,char *argv[])
-
{
-
int i=1;
-
string command;
-
-
// decode arguments
-
if(argc != 4) {
-
printf("USAGE: %s <mount dir> <username> <password>\n\n",argv[0]);
-
exit(0);
-
}
-
-
command = "sudo mount -t cifs //che-data.echostar.com/Shared " + argv[1] + "-ouser=" + argv[2] + ",pass=" + argv[3];
-
print(command);
-
system(command);
-
return(0);
-
}
-
-
error: invalid operands of types "const char [51]" and "std::string*" to binary "operator+"
I know what the problem is, I don't know how to fix it. I changed argv to string, no luck.
help?
Dan
| 
June 19th, 2009, 12:38 AM
|  | AdministratorVoR | | Join Date: Feb 2006 Location: South West UK
Posts: 6,117
Provided Answers: 6 | | | re: pass argument, concatenate string
Don't try and add everything together and then assign it to command in a single statement. Instead add everything to command using several statements.
| 
June 19th, 2009, 12:44 AM
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,075
| | | re: pass argument, concatenate string
Thanks I got that part working. I assigned and casted each of the argv[] to their own string var and then it worked.
But now I get an error when I try to pass the string 'command' to system() and it doesn't like it.
error: cannot convert "std::string" to "const char*" for argument "1" to "int system(const char*)"
Why can't system accept a string?
(Now I know why I love PHP)
Thanks Banfa for any help,
Dan
| 
June 19th, 2009, 07:08 AM
|  | Expert | | Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2 | | | re: pass argument, concatenate string Quote:
Originally Posted by dlite922 error: cannot convert "std::string" to "const char*" for argument "1" to "int system(const char*)"
Why can't system accept a string?
(Now I know why I love PHP) | Because it isn't written that way, it's a C function and strings don't exist in C; all you have to do is RTFM: here and you'll see that the c_str() function cures it all.
kind regards,
Jos
| 
June 19th, 2009, 05:34 PM
|  | Expert | | Join Date: Dec 2007 Location: Moon, Dark Side
Posts: 1,075
| | | re: pass argument, concatenate string
Thanks for your patience with a n00b. You would think the manuals would help newbies but if you think about it, it's pretty over whelming at first.
I was like that with the PHP manual, and now that's the only resource I use.
See you guys later,
Dan
| 
June 19th, 2009, 05:45 PM
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,338
| | | re: pass argument, concatenate string
Just use the combination assignment operator +=: - command += "sudo mount -t cifs //che-data.echostar.com/Shared ";
-
command += argv[1];
-
command += "-ouser=";
-
command += argv[2];
-
command += ",pass=";
-
command += argv[3];
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|