如何使用pstreams获得完整的流输出?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
流操作符>>旨在一次将一个单词读取到 std::string,同时跳过周围的空白,就像读取数字时一样。
如果您想一次读取整行文本,请使用 getline。
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.
这个问题实际上与我的 pstreams 库无关,它与 istream 有关,并且您从 ifstream 或其他 istream 读取时会得到完全相同的行为。但读取所有子进程输出的最简单方法是:
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:
解决方案,基于 Bo Persson 的提示:
[编辑:] 还有一个更简单的解决方案,同样基于 Bo 的评论:
Solution, based on Bo Persson's hint:
[Edit:] And an even simpler solution, again based on Bo's comments: