systeminfo如何打印行? [英] How does systeminfo print lines?

查看:52
本文介绍了systeminfo如何打印行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码在Windows 7上获取systeminfo。

I am using below code to get systeminfo on Windows 7.

#include<stdio.h>

int main()
{

    FILE *p;
    p = popen("systeminfo", "r");

    if(!p) {
        fprintf(stderr, "Error opening pipe.\n");
        return 1;
    }

    while(!feof(p)) {
        printf("%c", fgetc(p));
    }

    if (pclose(p) == -1) {
        fprintf(stderr," Error!\n");
        return 1;
    }

    return 0;
}

运行代码时,命令行会等待几秒钟以获取要求(提供)的信息。第一行说:

When the code is run, command line waits for a few seconds to get the required information. The first line says:

loading processor information...

然后将自身更新为:

loading hotfix information...

这种情况发生5到6次,然后在命令行窗口中写入实际输出。

This happens 5 or 6 times more and then actual output is written in command line window.

我想问一下第一行如何自我更新,尽管代码说要顺序打印所有字符?

I want to ask how this first line update itself although the code says to print all characters sequentially?

printf("%c", fgetc(p));


推荐答案

我做了一些实验,结果发现标准输出被重定向, systeminfo 会将状态行发送到标准错误。 (如果同时重定向标准输出和标准错误,则状态行将被抑制。)

I did some experimentation, and it turns out that if standard output is redirected, systeminfo sends the status lines to standard error instead. (If both standard output and standard error are redirected, the status lines are suppressed.)

由于 popen 仅重定向标准输出,您的代码在状态行中看不到字符,而是通过标准错误进入控制台。

Since popen only redirects standard output, your code isn't seeing the characters in the status lines, they're going to the console via standard error.

(请注意,对于子进程完全忽略重定向,并将其输出直接发送到控制台。)

(Note that it is also possible for a child process to ignore redirection altogether and send its output directly to the console.)

这篇关于systeminfo如何打印行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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