-1

我想将系统调用的输出重定向文件中..我试过这个..

#include <stdio.h>
int main()
{
        system("ls >> temp.txt");
        return 0;
}

但这仅适用于系统调用..如果我用其他东西代替ls,错误输出不会重定向到文件中..它只是打印在console..

例如

#include <stdio.h>
int main()
{
system("hello >> temp.txt");
return 0;
}

我希望将错误输出重定向到文件..我该怎么做?谢谢..

4

1 回答 1

3

它可以通过

system("hello > temp.txt 2>&1");

这会将标准输出和标准错误都定向到文件 temp.txt..

尽管

system("hello 2>&1 > temp.txt");

这只会将标准输出定向到 temp.txt..

笔记:

大于号不应与文件描述符的编号用空格分隔。如果将其分开,我们将再次将输出指向一个文件。

于 2013-04-30T09:10:41.123 回答