How to get full stream using pstream?

http://pstreams.sourceforge.net/ pstreams is apparently a very simple library, reimplementation of popen () for C ++.

The library is very easy to install, consisting of only one 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 I would rather simply: send a command to the system and get its output. The pstreams homepage (above) and the documentation offer the following example:

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

This seems to work well, but the code actually covers all the spaces and carriage returns from the output. Try the following:

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 concatenated looong string with no space.

, , :

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

.

.

:
++
++ POSIX?

: pstreams, - .

+3
3

→ std::string, , .

, getline .

std::getline(in, str);
+7

pstreams, istream, , ifstream istream. :

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

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;
}
0
source

All Articles