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?
fname, make itconst. Don't force the users of that function to work around your lack of const-correctness.