Global Variable Use

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. :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
 
char buff[512];
char arg[30];
char count_command[100];
string ser1();
string ser();
string status();
 
 
int main()
{
 
 
    FILE *val;
    if(!(val = popen("validate", "r")))
    {
        return 1;
    }
 
    while(fgets(buff, sizeof(buff), val)!=NULL)
    {
        std::string ser1=(buff);
        //ser= string(ser1);
//      ser=ser1;
 
//      ser=ser1.tostring();
        cout << ser1 << endl;
    }
    pclose(val);
/////////////////////////////////////////    Global Variable Re-use test
    cout << ser << endl;
 
 
 
///////////////////  OUTPUT Below:
 
00000000da472aae   
 
1  <----   This should be a duplicate of ser1 but this output is a cout of variable ser.
 
/////////////////
On line 9-11 you have declared three functions. If you want them to be variables you should remove the parentheses.
Line 9/10/11 are function prototypes. The function pointer ser is for some reason interpreted as a bool.

line 26 defines a local variable within the while loop (it shadows line 9). It will not be accessible outside.
Thanks for responding. I see my b0nehead error in how I setup the variables. I fixed it to look like:

1
2
3
4
5
6
char buff[512];
char arg[32];
char count_command;
string ser1;
string ser;
string status;


Now I get a compilation error when I run:
 
g++ check.cpp -o check


Error is:
check.cpp: In function ‘int main()’:
check.cpp:26:25: error: request for member ‘c_str’ in ‘buff’, which is of non-class type ‘char [512]’
c_str() is a member function of std::string. buff is a char array so it doesn't have any member functions.
Thanks Peter.

I managed to get past that error, and I've made progress building a command for another popen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <iostream>
#include <string>
using namespace 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.

Is there a better way?

Topic archived. No new replies allowed.