将命令行输出重定向到 C 中的变量

发布于 2025-01-19 14:54:12 字数 133 浏览 5 评论 0原文

有没有办法重定向命令行的输出,将整数作为输出返回到 C 中的变量?

例如,如果命令是“cmd”,那么有没有办法重定向其输出(整数)并将其存储在 C 中的变量中? 我尝试使用 popen 和 fgets 但它似乎只适用于字符。有什么建议吗?

Is there a way to redirect output of a command line which returns integer as an output to a variable in C?

for example, if the command is "cmd", then is there a way to redirect its output (an integer) and store it in variable in C?
I tried using popen and fgets but it seems to be working only with characters. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终陌 2025-01-26 14:54:12

它与 popen 和 fgets 完美配合:

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    const char *cmd = argc > 1 ? argv[1] : "echo 42";
    char buf[32];
    FILE *fp = popen(cmd, "r");
    if( fp == NULL ){
        perror("popen");
        return 1;
    }
    if( fgets(buf, sizeof buf, fp) == buf ){
        int v = strtol(buf, NULL, 10);
        printf("read: %d\n", v);
    }
    return 0;
}

It works perfectly fine with popen and fgets:

#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    const char *cmd = argc > 1 ? argv[1] : "echo 42";
    char buf[32];
    FILE *fp = popen(cmd, "r");
    if( fp == NULL ){
        perror("popen");
        return 1;
    }
    if( fgets(buf, sizeof buf, fp) == buf ){
        int v = strtol(buf, NULL, 10);
        printf("read: %d\n", v);
    }
    return 0;
}
遗心遗梦遗幸福 2025-01-26 14:54:12

如果你想从标准输入转换字符串,你可以使用 fgets ,然后使用 atoi 将输入转换为整数。

如果您想转换命令的输出,例如 ls 并将命令的输出存储到变量中,您可以了解 fork, dup2< /a>, 管道exec 函数系列。

有关此主题的更多信息,请参阅本教程:在 C 中捕获子进程的输出。如果您想保持“高水平”,本教程还提供了一个 popen 示例。

If you want to convert a character string from the standard input, you could use fgets and then use atoi to convert the input to an integer.

If you want to convert the output of a command, let's say ls and store the output of the command to a variable, you could learn about fork, dup2, pipe, and exec function family.

More about this topic on this tutorial : Capture the output of a child in C. This tutorial also provide an example with popen if you want to keep things "high level".

雾里花 2025-01-26 14:54:12

下面是一个使用 popen()fscanf() 的更简单的示例:

#include <errno.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    FILE *fp = popen("date '+%s'", "r");
    long seconds;
    if (fp == NULL) {
        fprintf(stderr, "popen failed: %s\n", strerror(errno));
        return 1;
    }
    if (fscanf(fp, "%ld", &seconds) == 1) {
        printf("epoch seconds: %ld\n", seconds);
        pclose(fp);
        return 0;
    } else {
        fprintf(stderr, "invalid program output\n");
        pclose(fp);
        return 1;
    }
}

Here is an even simpler example using popen() and fscanf():

#include <errno.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    FILE *fp = popen("date '+%s'", "r");
    long seconds;
    if (fp == NULL) {
        fprintf(stderr, "popen failed: %s\n", strerror(errno));
        return 1;
    }
    if (fscanf(fp, "%ld", &seconds) == 1) {
        printf("epoch seconds: %ld\n", seconds);
        pclose(fp);
        return 0;
    } else {
        fprintf(stderr, "invalid program output\n");
        pclose(fp);
        return 1;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文