C编程,如何防止puts写入控制台? [关闭]

问题描述 投票:-3回答:2

因此,假设您有一个C程序,在将控制权返回给int foo()之前先调用函数main(),然后main()使用puts()写入控制台。

防止puts写入控制台有哪些方法?

您获得的唯一#includes是stdio和stdlib。您无法触摸main()中的任何代码,只能触摸foo()

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

unsigned int foo()
{
    //Your code here

    return 0;
}

int main()
{

    foo();
    puts("If you are reading this you have failed");
    return 0;
}
c io-redirection
2个回答
3
投票

仅将redirect stdout移到其他位置(例如普通文件或字符设备,例如/dev/null)和freopen()。在此处查看示例

freopen()

使其适应您的特定情况应该很简单


1
投票

#include <stdio.h> #include <stdlib.h> int main(void) { puts("stdout is printed to console"); if (freopen("redir.txt", "w", stdout) == NULL) { perror("freopen() failed"); return EXIT_FAILURE; } puts("stdout is redirected to a file"); // this is written to redir.txt fclose(stdout); } 处,放置//Your code here

© www.soinside.com 2019 - 2024. All rights reserved.