Use of freopen functions (getting standard input and output streams from files) and freopen Input and Output
When solving acm problems, we usually need to run the program in the debugging environment (such as VC) after designing the algorithm and program, input the test data, and obtain the correct running result, submit the program to oj.
However, debugging often fails at a time, and test data must be re-input during each operation. For questions with a large amount of input data, it takes a lot of time to input the data. Using the freopen function can solve the problem of test data input and avoid repeated input, which is a simple and effective solution. Function Name: freopen Declaration: FILE * freopen (const char * path, const char * mode, FILE * stream); FILE: stdio. h parameter description: path: file name, used to store the custom file name of input and output. Mode: the mode in which the file is opened. The mode is the same as that in fopen (such as r-read-only and w-write. Stream: A file stream. Standard stream files are usually used. Return Value: success. A pointer to the file specified by path is returned. If no pointer is returned, NULL is returned. (Generally, you do not need to use its return value) function: Implements redirection to direct a predefined standard stream file to a file specified by path. Standard stream files refer to stdin, stdout, and stderr. Stdin is the standard input stream, the default is the keyboard; stdout is the standard output stream, the default is the screen; stderr is the standard error stream, generally set the screen as the default.The following is an example of debugging a + B Program under VC.C Syntax:
1 # include <stdio. h> 2 int main () 3 {4 int a, B; 5 freopen ("debug \ in.txt", "r", stdin ); // input redirection, the input data will be read from the in.txt file 6 freopen ("debug \ out.txt", "w", stdout); // output redirection, the output data will be stored in the out.txt file 7 while (scanf ("% d ", & a, & B )! = EOF) 8 printf ("% d \ n", a + B); 9 fclose (stdin); // close file 10 fclose (stdout ); // close file 11 return 0; 12}
C ++ syntax
# Include <stdio. h> # include <iostream. h> int main () {int a, B; freopen ("debug \ in.txt", "r", stdin ); // input redirection. The input data will be read from the in.txt file freopen ("debug \ out.txt", "w", stdout); // output redirection. The output data will be stored in the while (cin> a> B) file of out.txt) cout <a + B <endl; // use endl fclose (stdin); // close the file fclose (stdout); // close the file return 0 ;}
Freopen ("debug \ in.txt", "r", stdin) Redirects stdin to the debug \ in.txt file (keys are input from the keyboard). In this example, the input is obtained from the in.txt file when scanfor cininput is used, which is more convenient for debugging. Similarly, the function of freopen ("debug \ out.txt", "w", stdout) is to redirect stdout to the debug \ out.txt file. This will not be output to the console, and the output result will be opened to the out.txt file. Note: 1. in freopen ("debug \ in.txt", "r", stdin.pdf, the file directory is under the project directory. You can directly export the input file in.txt to the folder debug. the folder debug is the debugging folder automatically generated when the project file is created in VC.
If you change to freopen ("in.txt", "r", and stdin.pdf, The in.txt file will be placed in the created project folder. The in.txt file can also be placed in other folders, and the path can be correctly written. 2. You can view the output in the console without using output redirection. 3. After successful program debugging, do not forget to delete the redirection-related statements when submitting to oj.