如何读取C ++中的system()调用的结果? [英] How do I read the results of a system() call in C++?

查看:729
本文介绍了如何读取C ++中的system()调用的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码尝试在Linux中使用 popen 读取 df

I'm using the following code to try to read the results of a df command in Linux using popen.

#include <iostream> // file and std I/O functions

int main(int argc, char** argv) {
    FILE* fp;
    char * buffer;
    long bufSize;
    size_t ret_code;

    fp = popen("df", "r");
    if(fp == NULL) { // head off errors reading the results
        std::cerr << "Could not execute command: df" << std::endl;
        exit(1);
    }

    // get the size of the results
    fseek(fp, 0, SEEK_END);
    bufSize = ftell(fp);
    rewind(fp);

    // allocate the memory to contain the results
    buffer = (char*)malloc( sizeof(char) * bufSize );
    if(buffer == NULL) {
        std::cerr << "Memory error." << std::endl;
        exit(2);
    }

    // read the results into the buffer
    ret_code = fread(buffer, 1, sizeof(buffer), fp);
    if(ret_code != bufSize) {
        std::cerr << "Error reading output." << std::endl;
        exit(3);
    }

    // print the results
    std::cout << buffer << std::endl;

    // clean up
    pclose(fp);
    free(buffer);
    return (EXIT_SUCCESS);
}

这段代码给我一个内存错误,退出状态为 2,因此我可以看到其中出现问题,我只是不明白为什么。

This code is giving me a "Memory error" with an exit status of '2', so I can see where it's failing, I just don't understand why.

这一起来自我在 Ubuntu论坛 C ++参考资料,所以我没有结婚。如果任何人可以建议更好的方式来读取system()调用的结果,我会接受新的想法。

I put this together from example code that I found on Ubuntu Forums and C++ Reference, so I'm not married to it. If anyone can suggest a better way to read the results of a system() call, I'm open to new ideas.

编辑到原来的好吧, bufSize 是负面的,现在我明白为什么。你不能随机访问管道,因为我天真地尝试做。

EDIT to the original: Okay, bufSize is coming up negative, and now I understand why. You can't randomly access a pipe, as I naively tried to do.

我不能是第一个尝试这样做的人。有人可以给(或指向我)一个如何读取一个system()调用到C ++中的变量的结果的例子。

I can't be the first person to try to do this. Can someone give (or point me to) an example of how to read the results of a system() call into a variable in C++?

推荐答案

为什么 std :: malloc()失败?

显而易见的原因是因为 std :: ftell()返回一个负的有符号数,然后将其视为一个巨大的无符号数。

The obvious reason is "because std::ftell() returned a negative signed number, which was then treated as a huge unsigned number".

文档 std :: ftell( )在失败时返回-1。一个明显的原因是它会失败的是你不能在管道或FIFO中寻找。

According to the documentation, std::ftell() returns -1 on failure. One obvious reason it would fail is that you cannot seek in a pipe or FIFO.

没有转义;您不能知道命令输出的长度而不读取它,并且您只能读取一次。你必须以块的形式读取它,根据需要增加缓冲区或者在运行时解析。

There is no escape; you cannot know the length of the command output without reading it, and you can only read it once. You have to read it in chunks, either growing your buffer as needed or parsing on the fly.

但是,当然,你可以直接使用系统调用 df 可能使用获取其信息: statvfs()

But, of course, you can simply avoid the whole issue by directly using the system call df probably uses to get its information: statvfs().

这篇关于如何读取C ++中的system()调用的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆