Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to have option "addr" as variable.
As used now is a symbol...is it not ?

What is the proper syntax?

..as defined / used here

char addr[19] = { 0 };

ba2str(&(ii+i)->bdaddr, addr);




<pre lang="text">
<pre>QP->start("/bin/sh", { "-c", "echo q  | sudo -S hcitool info addr" });




What I have tried:

>QP->start("/bin/sh", { "-c", "echo q  | sudo -S hcitool info " + _ " addr" })
Posted

1 solution

QP->start("/bin/sh", { "-c", "echo q  | sudo -S hcitool info " + _ " addr" })


I'm not sure what the underscore is doing in there. Maybe a typo?

Why on earth would you think that addr inside a quoted string would refer to a program variable? If you had a variable info or echo in your program, would you expect the values of those to be substituted into the string as well????

Assuming std::string semantics for the arguments are for QP->start, then you should be able to use
C++
QP->start("/bin/sh", { "-c", "echo q  | sudo -S hcitool info " + addr });

If you the arguments are C style NUL terminated strings, then you've got options:
C++
// using std::string
std::string cmdstr{"echo q  | sudo -S hcitool info ");
cmdstr.append(addr);
QP->start("/bin/sh", { "-c", cmdstr.c_str() };

//using sprintf
#include <cstdio>
char cmdstr[256]; // something big enough to not overflow
sprintf(cmdstr, "echo q  | sudo -S hcitool info %s", addr);
QP->start("/bin/sh", { "-c", cmdstr};

//using strcat
#include <cstring>
char cmdstr[256]{"echo q  | sudo -S hcitool info "};
strcat(cmdstr, addr);
QP->start("/bin/sh", { "-c", cmdstr};

//using a string-stream
#include <sstream>
std::stringstream command{"echo q  | sudo -S hcitool info "};
comand << addr;
QP->start("/bin/sh", { "-c", command.str().c_str() };

There's probably others as well. I'm sure QString and friends provide similar facilities.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900