如何使用pstreams获得完整的流输出?

发布于 2024-10-22 03:48:29 字数 1468 浏览 5 评论 0原文

http://pstreams.sourceforge.net/ pstreams 显然是一个非常简单的库,为 C++ 重新实现了 popen()。

该库的安装非常简单,仅包含一个头文件。您可以在此处下载头文件并将其添加到您的应用程序中:
http://pstreams.cvs.sourceforge.net/ viewvc/pstreams/pstreams/pstream.h?view=log

我认为我想要的非常简单:向系统发送命令并获取其输出。 pstreams 的主页(上图)和文档提供了以下示例:

redi::ipstream in("ls ./*.h");
std::string str;
while (in >> str) {
    std::cout << str << std::endl;
}

它似乎运行良好,但代码实际上从输出中删除了所有空格和回车符。试试这个:

redi::ipstream in("ls -l"); /* The command has a slightly more complicated output. */
std::string str;
while (in >> str) {
    std::cout << str
}
std::cout << std::endl;

我们得到一个很长的没有任何空格的连接字符串。

我尽力理解文档,最接近的解决方案是:

 redi::ipstream in("ls -l");
 std::string str;
 while (!in.rdbuf()->exited()) {
 in >> str;
 }
 std::cout << str << std::endl;

但是,输出在第一个空格处被完全截断。

我很受阻。

相关问题:
c++ 中的 popen 等效项
如何执行命令并获取输出使用 POSIX 在 C++ 中使用命令?

版主请注意:此问题和上面的其他两个问题需要 pstreams 标签。

http://pstreams.sourceforge.net/
pstreams is an apparently very simple library, re-implementing popen() for C++.

The library is very simple to install, consisting of only one single header file. You can download the header file here and add it to your application:
http://pstreams.cvs.sourceforge.net/viewvc/pstreams/pstreams/pstream.h?view=log

I thought what I wanted was pretty straightforward: send a command to the system and get its output. The home page of pstreams (above) and the documentation offer the following example:

redi::ipstream in("ls ./*.h");
std::string str;
while (in >> str) {
    std::cout << str << std::endl;
}

It seems to work well, but the code actually strips all spaces and carriage return from the output. Try this:

redi::ipstream in("ls -l"); /* The command has a slightly more complicated output. */
std::string str;
while (in >> str) {
    std::cout << str
}
std::cout << std::endl;

and we get a looong concatenated string without any space.

I did my best to understand the documentation, and the closest I have come to a solution is this:

 redi::ipstream in("ls -l");
 std::string str;
 while (!in.rdbuf()->exited()) {
 in >> str;
 }
 std::cout << str << std::endl;

However, the output is completely truncated at the first space.

I am stymied.

Related questions:
popen equivalent in c++
How to execute a command and get output of command within C++ using POSIX?

Note to moderators: a pstreams tag is required for this question and the other two above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2024-10-29 03:48:29

流操作符>>旨在一次将一个单词读取到 std::string,同时跳过周围的空白,就像读取数字时一样。

如果您想一次读取整行文本,请使用 getline。

std::getline(in, str);

The streaming operator >> is designed to read a word at a time to a std::string, while skipping surrounding whitespace, just as you do when reading a number.

If you want to read an entire line of text at a time, use getline instead.

std::getline(in, str);
灵芸 2024-10-29 03:48:29

这个问题实际上与我的 pstreams 库无关,它与 istream 有关,并且您从 ifstream 或其他 istream 读取时会得到完全相同的行为。但读取所有子进程输出的最简单方法是:

redi::ipstream in("ls -l");
std::stringstream ss;
ss << in.rdbuf();
std::string s = ss.str();

The question is nothing to do with my pstreams library really, it's about istream and you'd get exactly the same behaviour reading from an ifstream or other istream. But the easiest way to read all the child process' output is:

redi::ipstream in("ls -l");
std::stringstream ss;
ss << in.rdbuf();
std::string s = ss.str();
夜雨飘雪 2024-10-29 03:48:29

解决方案,基于 Bo Persson 的提示:

#include <pstream.h>
#include <iostream>
#include <stdio.h>

int main() {
        redi::ipstream in("ls -l");
        std::string str;
        std::string s;
        while (std::getline(in, s)) {
                str += s + '\n';
        }
        std::cout << str;
        return 0;
}

[编辑:] 还有一个更简单的解决方案,同样基于 Bo 的评论:

include <pstream.h>
#include <iostream>
#include <stdio.h>

int main() {
        redi::ipstream in("ls -l");
        std::string str;
        std::getline(in, str, '\0');
        std::cout << str;
        return 0;
}

Solution, based on Bo Persson's hint:

#include <pstream.h>
#include <iostream>
#include <stdio.h>

int main() {
        redi::ipstream in("ls -l");
        std::string str;
        std::string s;
        while (std::getline(in, s)) {
                str += s + '\n';
        }
        std::cout << str;
        return 0;
}

[Edit:] And an even simpler solution, again based on Bo's comments:

include <pstream.h>
#include <iostream>
#include <stdio.h>

int main() {
        redi::ipstream in("ls -l");
        std::string str;
        std::getline(in, str, '\0');
        std::cout << str;
        return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文