<tfoot id='qy3Jq'></tfoot>

  • <i id='qy3Jq'><tr id='qy3Jq'><dt id='qy3Jq'><q id='qy3Jq'><span id='qy3Jq'><b id='qy3Jq'><form id='qy3Jq'><ins id='qy3Jq'></ins><ul id='qy3Jq'></ul><sub id='qy3Jq'></sub></form><legend id='qy3Jq'></legend><bdo id='qy3Jq'><pre id='qy3Jq'><center id='qy3Jq'></center></pre></bdo></b><th id='qy3Jq'></th></span></q></dt></tr></i><div id='qy3Jq'><tfoot id='qy3Jq'></tfoot><dl id='qy3Jq'><fieldset id='qy3Jq'></fieldset></dl></div>

      <small id='qy3Jq'></small><noframes id='qy3Jq'>

      <legend id='qy3Jq'><style id='qy3Jq'><dir id='qy3Jq'><q id='qy3Jq'></q></dir></style></legend>
        • <bdo id='qy3Jq'></bdo><ul id='qy3Jq'></ul>

        在 C++ 中读取 popen 结果

        时间:2024-08-14

          <legend id='N4io7'><style id='N4io7'><dir id='N4io7'><q id='N4io7'></q></dir></style></legend>

          <small id='N4io7'></small><noframes id='N4io7'>

        1. <i id='N4io7'><tr id='N4io7'><dt id='N4io7'><q id='N4io7'><span id='N4io7'><b id='N4io7'><form id='N4io7'><ins id='N4io7'></ins><ul id='N4io7'></ul><sub id='N4io7'></sub></form><legend id='N4io7'></legend><bdo id='N4io7'><pre id='N4io7'><center id='N4io7'></center></pre></bdo></b><th id='N4io7'></th></span></q></dt></tr></i><div id='N4io7'><tfoot id='N4io7'></tfoot><dl id='N4io7'><fieldset id='N4io7'></fieldset></dl></div>
              <tbody id='N4io7'></tbody>
            <tfoot id='N4io7'></tfoot>

                • <bdo id='N4io7'></bdo><ul id='N4io7'></ul>
                  本文介绍了在 C++ 中读取 popen 结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个 C++ 应用程序,我需要读取系统命令的结果.

                  I am writing a C++ application and I need to read the result of a system command.

                  我或多或少地使用 popen() 如下所示:

                  I am using popen() more or less as shown here:

                      const int MAX_BUFFER = 2048;
                      string cmd="ls -l";
                      char buffer[MAX_BUFFER];
                      FILE *stream = popen(cmd.c_str(), "r");
                      if (stream){
                         while (!feof(stream))
                         {
                              if (fgets(buffer, MAX_BUFFER, stream) != NULL)
                              {
                                 //here is all my code
                              }
                         }
                         pclose(stream);
                      }
                  

                  我一直在尝试以不同的方式重新编写它.我看到了一些非标准的解决方案,例如:

                  I've been trying to re-write this in a different way. I saw some non-standard solutions like:

                  FILE *myfile;
                  std::fstream fileStream(myfile);
                  std::string mystring;
                  while(std::getline(myfile,mystring))
                  {
                      // .... Here I do what I need
                  }
                  

                  我的编译器不接受这个.

                  My compiler does not accept this though.

                  如何从 C++ 中的 popen 读取数据?

                  How can I read from popen in C++?

                  推荐答案

                  你的例子:

                  FILE *myfile;
                  std::fstream fileStream(myfile);
                  std::string mystring;
                  while(std::getline(myfile,mystring))
                  

                  不起作用,因为尽管您非常接近,但标准库不提供可以从 FILE* 构造的 fstream.Boost iostreams 确实提供了一个iostream 可以从文件描述符构造,您可以通过调用 filenoFILE* 获取一个.

                  Does't work because although you're very close the standard library doesn't provide an fstream that can be constructed from a FILE*. Boost iostreams does however provide an iostream that can be constructed from a file descriptor and you can get one from a FILE* by calling fileno.

                  例如:

                  typedef boost::iostreams::stream<boost::iostreams::file_descriptor_sink>
                          boost_stream; 
                  
                  FILE *myfile; 
                  // make sure to popen and it succeeds
                  boost_stream stream(fileno(myfile));
                  stream.set_auto_close(false); // https://svn.boost.org/trac/boost/ticket/3517
                  std::string mystring;
                  while(std::getline(stream,mystring))
                  

                  不要忘记稍后pclose.

                  注意:较新版本的 boost 已经弃用了只需要一个 fd 的构造函数.相反,您需要将 boost::iostreams::never_close_handleboost::iostreams::close_handle 之一作为强制第二个参数传递给构造函数.

                  Note: Newer versions of boost have deprecated the constructor which takes just a fd. Instead you need to pass one of boost::iostreams::never_close_handle or boost::iostreams::close_handle as a mandatory second argument to the constructor.

                  这篇关于在 C++ 中读取 popen 结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:自定义流到 C++ 中的方法? 下一篇:将 std::cout 重定向到自定义编写器

                  相关文章

                  • <bdo id='6gR3X'></bdo><ul id='6gR3X'></ul>
                  <i id='6gR3X'><tr id='6gR3X'><dt id='6gR3X'><q id='6gR3X'><span id='6gR3X'><b id='6gR3X'><form id='6gR3X'><ins id='6gR3X'></ins><ul id='6gR3X'></ul><sub id='6gR3X'></sub></form><legend id='6gR3X'></legend><bdo id='6gR3X'><pre id='6gR3X'><center id='6gR3X'></center></pre></bdo></b><th id='6gR3X'></th></span></q></dt></tr></i><div id='6gR3X'><tfoot id='6gR3X'></tfoot><dl id='6gR3X'><fieldset id='6gR3X'></fieldset></dl></div>

                • <small id='6gR3X'></small><noframes id='6gR3X'>

                  <legend id='6gR3X'><style id='6gR3X'><dir id='6gR3X'><q id='6gR3X'></q></dir></style></legend>

                      <tfoot id='6gR3X'></tfoot>