且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

内部捕捉/重定向标准输出?

更新时间:2022-12-09 11:28:29

您可以使用则freopen 来改变描述符。

You can use freopen to change the descriptor.

#include<stdio.h>

main(int argc, char** argv) {
    FILE *fp = freopen("output.txt", "w", stdout);
    printf("Hello\n");
    fclose(fp);
}

如果您运行,你会看到output.txt的并没有什么与printf输出会去到你的屏幕。

If you run that you'll see the printf output in output.txt and nothing will go to your screen.

您现在可以打开读取数据文件或者你甚至可以 MMAP 它到您的存储空间和处理它的方式。

You can now open the file to read the data or you could even mmap it into your memory space and process it that way.