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

查看:17
本文介绍了如何在 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 论坛a href="http://en.cppreference.com/w/cpp/io/c/fread" rel="nofollow noreferrer">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天全站免登陆