我試圖編譯下面的代碼(從https://stackoverflow.com/a/478960/683218)。 的編譯就OK,如果我mingw:使用-std = C++編譯時找不到函數11
$ g++ test.cpp
編譯但是當-std=c++11
開關使用了錯誤:
$ g++ -std=c++11 test.cpp
test.cpp: In function 'std::string exec(char*)':
test.cpp:6:32: error: 'popen' was not declared in this scope
FILE* pipe = popen(cmd, "r");
^
任何想法是怎麼回事?
(我用從mingw.org mingw32的gcc4.8.1,並WindowsXP64)
代碼:
#include <string>
#include <iostream>
#include <stdio.h>
std::string exec(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
int main() {}
完全無關的*電流*的問題,但不這樣做''時,它不會像您期望的工作(FEOF(...)!)它來。原因是因爲在你試圖從文件末尾讀取之後,EOF標誌將不會被設置,直到*之後,所以你將迭代一次到多次。相反,在你的情況下,只需'while(fgets(...)!= 0)'。從C++流中讀取時也是如此。 –