[英]return char from std::string vector
如何傳遞popen數據? 我有一個腳本,但是每當我嘗試將數據帶入另一個函數時,都會遇到轉換錯誤->從字符串常量到'char *'的棄用轉換,因為popen希望使用標准char。
碼:
#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 );
}
錯誤:
#g ++ -g returnFileP.cpp -o returnFileP.o -std = gnu ++ 11 returnFileP.cpp:在函數'int main()'中:returnFileP.cpp:29:66:警告:不建議將字符串常量轉換為'char *'[-Wwrite-strings]
如何正確將Popen數據傳遞/返回給另一個函數?
在main
調用init
時出錯。 字符串文字"/usr/bin/php getMyorders.php 155"
類型為const char *
,調用init要求隱式轉換為char *
。 允許這種轉換(用於字符串文字),但現在不建議使用。
popen
的第一個參數的類型為const char *
,因此我不知道為什么init
要求使用非const參數的原因。 聲明為
FILE *init( const char *fname )
擺脫警告。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.