bro
bro

Reputation: 23

return char from std::string vector

How do you pass popen data? I have a script I use but when ever I try and bring the data into another function, I get a conversion error -> deprecated conversion from string constant to 'char*' because popen want to be in standard char.

code:

#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>

using namespace std;

FILE *init( char *fname ){
        FILE *fp = popen( fname, "r" );
        return fp;
}

char getmarketbuyData(FILE *fp){
        char buff[BUFSIZ];
        vector<std::string> vrecords;
        while(std::fgets(buff, sizeof buff, fp) != NULL){
                size_t n = std::strlen( buff );
                if ( n && buff[n-1] == '\n' ) buff[n-1] = '\0';
                if ( buff[0] != '\0' ) vrecords.push_back( buff );
        }
        for(int t = 0; t < vrecords.size(); ++t){
                cout << vrecords[t] << " " << endl;
        }
                return 0;
}

int main(void){
        FILE *fp = NULL;
        fp = init("/usr/bin/php getMyorders.php 155");
        if (fp == NULL) perror ("Error opening file");
        if ( fp )
                getmarketbuyData( fp );
}

error:

# g++ -g returnFileP.cpp -o returnFileP.o -std=gnu++11 returnFileP.cpp: In function 'int main()': returnFileP.cpp:29:66: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]

How do I properly pass/return popen data to another function?

Upvotes: 0

Views: 147

Answers (1)

Mikhail Maltsev
Mikhail Maltsev

Reputation: 1678

You are getting an error when calling init in main. The string literal "/usr/bin/php getMyorders.php 155" has type const char * and call to init requires an implicit conversion to char *. Such conversion (for string literals) was allowed, but it's now deprecated.

popen's first argument has type const char *, so I don't see a reason why should init require a non-const parameter. Declare it as

FILE *init( const char *fname )

to get rid of warnings.

Upvotes: 1

Related Questions