error C3861: popen, pclose: identifier not found in C++

huangapple go评论59阅读模式
英文:

error C3861: popen, pclose: identifier not found in c++

问题

I get the error "error C3861: popen: identifier not found", if I run through vs code then everything works, but if I run through Microsoft Visual Studio, then the program does not want to compile. How to fix it?

code (C++):

#include <iostream>
#include <string>
#include <cstdlib>

int main() {
    std::string result = "";

    FILE* pipe = popen("some command 2>&1", "r"); //error

    char buffer[128];
    while (!feof(pipe)) {
        if (fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe); //error

    std::cout << "res: " << result;
    return 0;
}
英文:

I get the error "error C3861: popen: identifier not found", if I run through vs code then everything works, but if I run through microsoft vs then the program does not want to compile. How to fix it?

code (c++):

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt;

int main() {
    std::string result = &quot;&quot;;

    FILE* pipe = popen(&quot;some command 2&gt;&amp;1&quot;, &quot;r&quot;); //error

    char buffer[128];
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL)
            result += buffer;
    }
    pclose(pipe); //error

    std::cout &lt;&lt; &quot;res :&quot; &lt;&lt; result;
    return 0;
}

答案1

得分: 4

如果您使用Windows,可能会使用_popen()和_pclose()。

尝试这样做:

FILE* pipe = _popen("some command 2>&1", "r");
英文:

If you are on Windows, it might be _popen() and _pclose()

Try this:

FILE* pipe = _popen(&quot;some command 2&gt;&amp;1&quot;, &quot;r&quot;);

huangapple
  • 本文由 发表于 2023年2月27日 19:35:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定