Hi, I'm trying to reuse a global variable as a string. Basically, I use popen to call a system command, and take that command's output as the contents of a char array. I then want to set the car array to a global string variable so I can use that string to build another command. My example doesn't seem to work. I could use some help. :)
#include <stdio.h>
#include <iostream>
#include <string>
usingnamespace std;
char buff[512];
char buff2[512];
char arg[32];
char count_command;
string ser1;
string ser;
string status;
int main()
///// Get serial number and pass to a string var
{
FILE *val;
if(!(val = popen("validate", "r")))
{
return 1;
}
while(fgets(buff, sizeof(buff), val)!=NULL)
{
std::string ser1=(buff);
ser=ser1;
}
pclose(val);
///// remove carriage return from returned serial number
string::size_type pos = 0;
while ( ( pos = ser.find ("\n",pos) ) != string::npos )
{
ser.erase ( pos, 2 );
}
///// Build command for curl to execute to contact the webserver and retrieve status
std::string curl_comm="curl -ks --data \"search="+ser+"\" https:\057\057apps.somewebsite:2443/proc.php";
cout << curl_comm << endl; // test curl_comm
///// Query app server - provide serial and get response to a string var
FILE *fin;
if(!(fin = popen(curl_comm, "r")))
{
return 1;
}
while(fgets(buff2, sizeof(buff2), fin)!=NULL)
{
std::string status(buff2);
cout << status;
}
pclose(fin);
///// End Of MAIN
return 0;
}
Upon compilation, I now get this error:
g++ check.cpp -o check
check.cpp: In function ‘int main()’:
check.cpp:43:36: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘FILE* popen(const char*, const char*)’
I'm assuming because I am trying to pass a string as a command and not a char array to the second popen section? My goal is to build the command, as I have done and have this command run, then take the output and stick it in a string variable so that some decision making can happen down the road.