Question

Supposons que je « popen » un exécutable, je reçois un FILE* en retour. De plus, supposons que je voudrais « connecter » ce fichier à un objet istream pour faciliter le traitement, est-il un moyen de le faire?

Était-ce utile?

La solution

Il n'y a aucun moyen standard mais si vous voulez une solution rapide, vous pouvez obtenir le descripteur de fichier avec fileno () puis utilisez Josuttis' fdstream . Il peut y avoir des efforts similaires autour mais j'utilisé cela dans le passé lointain et il a bien fonctionné. Si rien d'autre, il devrait être une très bonne carte pour mettre en œuvre votre propre.

Autres conseils

Vous pouvez vous en dérivant std :: basic_streambuf ou std :: cours streambuf.
Quelque chose le long de ces lignes:

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

#define BUFFER_SIZE     1024

class popen_streambuf : public std::streambuf {
public:
    popen_streambuf() : fp(NULL) {
    }
    ~popen_streambuf() {
        close();
    }
    popen_streambuf *open(const char *command, const char *mode) {
        fp = popen(command, mode);
        if (fp == NULL)
            return NULL;
        buffer = new char_type[BUFFER_SIZE];
        // It's good to check because exceptions can be disabled
        if (buffer == NULL) {
            close();
            return NULL;
        }
        setg(buffer, buffer, buffer);
        return this;
    }
    void close() {
        if (fp != NULL) {
            pclose(fp);
            fp = NULL;
        }
    }
    std::streamsize xsgetn(char_type *ptr, std::streamsize n) {
        std::streamsize got = showmanyc();
        if (n <= got) {
            memcpy(ptr, gptr(), n * sizeof(char_type));
            gbump(n);
            return n;
        }
        memcpy(ptr, gptr(), got * sizeof(char_type));
        gbump(got);

        if (traits_type::eof() == underflow()) {
            return got;
        }
        return (got + xsgetn(ptr + got, n - got));
    }
    int_type underflow() {
        if (gptr() == 0) {
            return traits_type::eof();
        }
        if (gptr() < egptr()) {
            return traits_type::to_int_type(*gptr());
        }
        size_t len = fread(eback(), sizeof(char_type), BUFFER_SIZE, fp);
        setg(eback(), eback(), eback() + (sizeof(char_type) * len));
        if (0 == len) {
            return traits_type::eof();
        }
        return traits_type::to_int_type(*gptr());
    }
    std::streamsize showmanyc() {
        if (gptr() == 0) {
           return 0;
        }
        if (gptr() < egptr()) {
            return egptr() - gptr();
        }
        return 0; 
    }
private:
    FILE *fp;
    char_type *buffer;
};

int main(int argc, char *argv)
{
    char c;
    popen_streambuf sb;
    std::istream is(&sb);

    if (NULL == sb.open("ls -la", "r")) {
        return 1;
    }

    while (is.read(&c, 1)) {
        std::cout << c;
    }

    return 0;
}

Bien sûr, il y a un moyen, mettre en œuvre votre propre istream qui peut être construit à partir d'un FILE*.

Si vous demandez s'il y a un moyen standard pour ce faire, alors non.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top