Connecting Tech Pros Worldwide Forums | Help | Site Map

pass argument, concatenate string

dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#1: Jun 18 '09
All i want to do in C++ is pass a few arguments, concatenate them into variable and call system()

I've gotten this far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc,char *argv[])
  9. {
  10.     int i=1;
  11.     string command;
  12.  
  13. //  decode arguments
  14.     if(argc != 4) {
  15.         printf("USAGE: %s <mount dir> <username> <password>\n\n",argv[0]);
  16.         exit(0);
  17.     }
  18.  
  19.     command = "sudo mount -t cifs //che-data.echostar.com/Shared " + argv[1] + "-ouser=" + argv[2] + ",pass=" + argv[3];
  20.     print(command); 
  21.     system(command); 
  22.     return(0);
  23. }
  24.  
  25.  
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

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,195
#2: Jun 19 '09

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.
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#3: Jun 19 '09

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
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 19 '09

re: pass argument, concatenate string


Quote:

Originally Posted by dlite922 View Post

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
dlite922's Avatar
Expert
 
Join Date: Dec 2007
Location: Moon, Dark Side
Posts: 1,095
#5: Jun 19 '09

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
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,379
#6: Jun 19 '09

re: pass argument, concatenate string


Just use the combination assignment operator +=:

Expand|Select|Wrap|Line Numbers
  1. command += "sudo mount -t cifs //che-data.echostar.com/Shared ";
  2. command += argv[1];
  3. command += "-ouser=";
  4. command +=  argv[2];
  5. command += ",pass=";
  6. command += argv[3]; 
Reply