Freopen-C/C ++ file input and output Tool

Source: Internet
Author: User

Freopen is often used in the past and is convenient. It can be used as a template and replaced with your ownCode.

 
# Include <stdio. h> // in actual use, it is found that freopen is also included in iostream. h. c ++ Code # include <iostream. h>. Int main () {freopen ("sample. in "," r ", stdin); freopen (" sample. out "," W ", stdout);/* same as the console input and output */fclose (stdin); fclose (stdout); Return 0 ;}

----- From: http://www.slyar.com/blog/c-freopen-stdin-stdout.html
 -------------

When solving ACM questions, we usually DesignAlgorithmAndProgramIn the debugging environment (such as VC
Run the program, enter the test data, and submit the program to OJ only after the correct running result is obtained. However, because debugging often fails once, you must re-enter the test data during each operation.
For a question with a large amount of input data, it takes a lot of time to input 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, usually using a standard stream file.
Return Value: success. A pointer to the file specified by path is returned. If no pointer is returned, null is returned. (Generally, the return value is not used)
Function: Implement 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:
# Include <stdio. h>
Int main ()
{
Int A, B;
Freopen ("Debug \ in.txt", "r", stdin); // the input data is read from the in.txt file.
Freopen ("Debug \ out.txt", "W", stdout); // output redirection, the output data will be stored in the out.txt File
While (scanf ("% d", & A, & B )! = EOF)
Printf ("% d \ n", A + B );
Fclose (stdin); // close the file
Fclose (stdout); // close the file
Return 0;
}

C ++ syntax
# Include <stdio. h>
# Include <iostream. h>
Int main ()
{
Int A, B;
Freopen ("Debug \ in.txt", "r", stdin); // the input data is read from the in.txt file.
Freopen ("Debug \ out.txt", "W", stdout); // output redirection, the output data will be stored in the out.txt File
While (CIN> A> B)
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 the standard input stream stdin to the debug \ in.txt file.
When scanfis used or cininput is used, data is not read from the standard input stream, but the input is obtained from the in.txt file. You only need to paste the input data to in.txt.
More.
Similarly, freopen ("Debug \ out.txt", "W", stdout) is used to redirect stdout to the debug \ out.txt file. In this example, open the out.txt file to view the output result.

Note:
1.
In freopen ("Debug \ in.txt", "r", stdin.pdf, place the input file in.txt In the debug folder, 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 the program is successfully debugged, do not forget to delete the redirection-related statements when submitting to OJ.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.