0

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?

1
  • 2
    If you aren't modifying fname, make it const. Don't force the users of that function to work around your lack of const-correctness. Commented Nov 23, 2014 at 5:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.