freopen( 二 )

其它作业系统中使用:freopen( "/dev/tty", "w", stdout );Windows代码举例#include<stdio.h>#include<stdlib.h>int main(){    FILE *stream;    if ((stream = freopen("file.txt", "w", stdout)) == NULL)        exit(-1);    printf("this is stdout output\n");    stream = freopen("CON","w",stdout);    /*stdout是向程式的末尾的控制台重定向*/    printf("And now back to the console once again\n");    return 0;}Linux代码举例【freopen】#include <stdio.h>#include <stdlib.h>int main(void){    FILE *stream;    if ((stream = freopen("file.txt", "w", stdout)) == NULL)        exit(-1);    printf("this is stdout output\n");    stream = freopen("/dev/tty","w",stdout);    /*stdout是向程式的末尾的控制台重定向*/    printf("And now back to the console once again\n");    return 0;}警告:在使用上述方法在输入输出流间进行反覆的重定向时,极有可能导致流指针得到不被期待的结果,使输入输出发生异常,所以如果需要在档案的输入输出和标準输入输出流之间进行切换,建议使用fopen或者是C++标準的ifstream及ofstream 。