在窗口应用中使用printf,cout 等将输出重定向到console

本文介绍如何使用C++将控制台的输入输出重定向至文件,包括实例演示如何将标准输入、输出重定向到指定的文本文件中,以及如何在程序中读取和写入文件内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[cpp]  view plain copy
  1. #include <conio.h>   
  2. #include <iostream>   
  3. #include <io.h>   
  4. #include <fcntl.h>   
  5. #include <Windows.h>   
  6. using namespace std ;   
  7. int _tmain (int argc , _TCHAR * argv [])   
  8. {   
  9.     AllocConsole ();                      // 为一个进程定位一个console ,如果是win32 程序的话这里就派上用场了   
  10.     //Retrieves a handle to the specified standard device (standard input, standard output, or standard error).   
  11.     HANDLE hin = ::GetStdHandle (STD_INPUT_HANDLE );                           
  12.     HANDLE hout = ::GetStdHandle (STD_OUTPUT_HANDLE );   
  13.     //Associates a C run-time file descriptor with an existing operating-system file handle.   
  14.     int hcin = _open_osfhandle ((intptr_t )hin ,_O_TEXT );                 // 此时hcin 就成了一个file descriptor 了   
  15.     //      When a program opens a file, the operating system returns a corresponding file descriptor that the program refers to   
  16.     //     in order to process the file. A file descriptor is a low positive integer. The first three file descriptors (0,1, and 2,)   
  17.     //     are associated with the standard input (stdin), the standard output (stdout), and the standard error (stderr), respectively.   
  18.     //     Thus, the function scanf() uses stdin and the function printf() uses stdout. You can override the default setting and   
  19.     //     re-direct the process's I/O to different files by using different file descriptors:   
  20.     //     #include <cstdio>   
  21.     //     fprintf(stdout, "writing to stdout"); //write to stdout instead of a physical file   
  22.     FILE * fpin = _fdopen (hcin ,"r" );   
  23.     *stdin = *fpin ;                                                  //stdin 就指向了文件指针   
  24.     int hcout = _open_osfhandle ((intptr_t )hout ,_O_TEXT );   
  25.     FILE * fpout = _fdopen (hcout ,"wt" );   
  26.     *stdout = *fpout ;   
  27.     std ::ios_base ::sync_with_stdio ();           // 将iostream 流同c runtime lib 的stdio 同步,标准是同步的   
  28.     printf ("hello,world" );   
  29.     std ::cout << "test" ;   
  30.     int i ;   
  31.     std ::cin >> i ;  
  32.     std ::cout << i ;   
  33.     return 0;   
  34. }   

鉴于以上的说明,这里可以在扩展一下,既然 *stdin  = *fpin ;能够将stdin定向到文件指针,那么我同样可以实现将stdin,stdout重定向到文件的功能,并且只需做很少的改动:

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include <conio.h>  
  3. #include <iostream>  
  4. #include <io.h>  
  5. #include <fcntl.h>  
  6. #include <Windows.h>  
  7. using namespace std ;  
  8. int _tmain (int argc , _TCHAR * argv [])  
  9. {  
  10.     AllocConsole ();                            // 为一个进程定位一个 console ,如果是 win32 程序的话这里就派上用场了  
  11.     FILE * fpin = fopen ("c://in.txt" ,"r" );  
  12.     *stdin = *fpin ;                                                //stdin 就指向了文件指针  
  13.     FILE * fpout = fopen ("c://out.txt" ,"wt" );  
  14.     *stdout = *fpout ;  
  15.     std ::ios_base ::sync_with_stdio ();            // 将 iostream 流同 c runtime lib 的 stdio 同步,标准是同步的  
  16.     printf ("hello,world" );  
  17.     std ::cout << "test" ;  
  18.     int i ;  
  19.     std ::cin >> i ;  
  20.     std ::cout << i ;  
  21.     return 0;  
  22. }   

有没有发现,如此产生的效果是惊人的!

   FILE * fpin = fopen ("c://in.txt" ,"r" );
   *stdin = *fpin ;

这两句将标准输入定位到了文件,当面临需要在控制台输入大量字符串,数字等的情况,例如输入变换矩阵什么的,此时使用这个就太方便了,直接将矩阵写入txt文件就可。

 

另一种简单的将stdin,out重定向的方法:

[cpp]  view plain copy
  1. AllocaConsole();  
  2. freopen("CONIN$""r+t", stdin);  
  3. freopen("CONOUT$""w+t", stdout);  
  4. ....  
  5. freeConsole();  

更多 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值