# Include <conio. h> <br/> # include <iostream> <br/> # include <Io. h> <br/> # include <fcntl. h> <br/> # include <windows. h> <br/> using namespace STD; <br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> allocconsole (); // locate a console for a process, if it is a Win32 program, it will be used here <br/> // retrieves a handle to the specified standard device (standard input, standard output, or standard error ). <br/> handle hin =: getstdhandle (std_input_handle); <br/> handle Hout =: getstdhandle (std_output_handle ); <br/> // associates a C run-time file descriptor with an existing operating-system file handle. <br/> int hcin = _ open_osfhandle (intptr_t) Hin, _ o_text ); // hcin becomes a file descriptor <br/> // when a program opens a file, the operating system returns a corresponding file descriptor that the program refers to <br/> // In order to process the file. A file descriptor is a low positive integer. the first three file descriptors (0, 1, and 2,) <br/> // are associated with the standard input (stdin), the standard output (stdout ), and the standard error (stderr), respectively. <br/> // Thus, the function scanf () uses stdin and the function printf () uses stdout. you can override the default setting and <br/> // re-direct the process's I/O to different files by using different file descriptors: <br/> // # include <cstdio> <br/> // fprintf (stdout, "writing to stdout "); // write to stdout instead of a physical file <br/> file * fpin = _ fdopen (hcin, "R"); <br/> * stdin = * fpin; // stdin points to the file pointer <br/> int hcout = _ open_osfhandle (intptr_t) Hout, _ o_text); <br/> file * fpout = _ fdopen (hcout, "WT"); <br/> * stdout = * fpout; <br/> STD: ios_base: sync_with_stdio (); // synchronize the iostream with the stdio of C Runtime Lib. The standard is synchronous <br/> printf ("Hello, world"); <br/> STD :: cout <"test"; <br/> int I; <br/> STD: CIN> I; <br/> STD: cout <I; <br/> return 0; <br/>}< br/>
In view of the above description, we can extend it here. Since * stdin = * fpin; can direct stdin to file pointers, I can also implement stdin, stdout redirects to a file and requires few changes:
# Include "stdafx. H "<br/> # include <conio. h> <br/> # include <iostream> <br/> # include <Io. h> <br/> # include <fcntl. h> <br/> # include <windows. h> <br/> using namespace STD; <br/> int _ tmain (INT argc, _ tchar * argv []) <br/>{< br/> allocconsole (); // locate a console for a process, if it is a Win32 program, it will be used here <br/> file * fpin = fopen ("C: // in.txt", "R "); <br/> * stdin = * fpin; // stdin points to the file pointer <br/> file * fpout = fopen ("C: // out.txt ", "WT"); <br/> * stdout = * fpout; <br/> STD: ios_base: sync_with_stdio (); // synchronize the iostream with the stdio of C Runtime Lib. The standard is synchronous <br/> printf ("Hello, world"); <br/> STD :: cout <"test"; <br/> int I; <br/> STD: CIN> I; <br/> STD: cout <I; <br/> return 0; <br/>}
Have you found that the effect is amazing!
File * fpin = fopen ("C: // in.txt", "R ");
* Stdin = * fpin;
These two sentences locate the standard input to the file. When you need to input a large number of strings and numbers in the console, such as the input transformation matrix or something, it is too convenient to use this sentence, write the Matrix directly to the TXT file.
Another simple method to redirect stdin and out is as follows:
Allocaconsole (); <br/> freopen ("conin $", "R + T", stdin); <br/> freopen ("conout $ ", "W + T", stdout); <br/> .... <br/> freeconsole ();