Use of freopen () function in ACM.

Source: Internet
Author: User

We need to debug the ACM on a local machine. In the debugging process, it is acceptable if there is less input data, but if there is a large amount of input data, we can hardly bear to re-input and debug it again and again. Use Google to find a simple method, that is, the freopen function.
Using the freopen function can solve the problem of test data input and avoid repeated input, which is a simple and effective solution.
The following is an introduction to the function, see http://www.cplusplus.com/reference/clibrary/cstdio/freopen.html for details
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. By calling freopen, you can modify the default value of the standard stream file to achieve redirection.

The following examples are provided in vs2005 for verification:

# Include <cstdio>

# Include <iostream>

Using namespace STD;

Int main ()

{

Int A, B;

Freopen ("in.txt", "r", stdin); // input redirection, the input data will be read from the in.txt File

Freopen ("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 ("in.txt", "R" records file to obtain the input. You only need to paste the input data to in.txt first, which makes debugging much easier.
Similarly, freopen ("out.txt", "W", stdout)redirects stdoutto the out.txt file. In this example, open the out.txt file to view the output result.
Note:
1. When calling the freopen function to implement redirection, the path name must be correctly written.
2. You can choose not to use input redirection. You can still use the keyboard input or output redirection. You can still view the output in the console. This is all possible, depending on your needs.
3. This method is suitable for upgrading the trial on the local machine. After successful program debugging, do not forget to delete the redirection-related statements when submitting to OJ. In fact, this problem is still taken into account by the OJ system of some schools. For example, the OJ System of hangdian has this problem in the FAQ:

Q: Is there any way to determine if my program is runned at online judge or not?

A: There is a conditional define of compiler calledOnline_judge. Example of using:

C/C ++

# Ifdef online_judge

Running on online judge

# Else

You can do something here on your local computer

# Endif

For example, I can submit hangdian 1000 questions as follows:

# Include <cstdio>

# Include <iostream>

Using namespace STD;

Int main ()

{

# Ifdef online_judge

# Else

Freopen ("in.txt", "r", stdin );

# Endif

Int A, B;

While (CIN> A> B)

Cout <a + B <Endl;

Return 0;

}

During local machine debugging, because online_judge has not been defined, freopen ("in.txt", "r", stdin) will be executed to facilitate local debugging, when submitted to OJ, the statement is skipped because online_judge is defined.

Freopen ("in.txt", "r", stdin); starts from int A, B. Tested, AC.

After designing algorithms and programs, you need to run the program in the debugging environment (such as Vc), input the test data, and submit the program to OJ only after obtaining the correct running result. 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, 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.
For example:

# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char ch;
Freopen ("a.txt", "r", stdin );

While (CH = getchar ()! = '\ N ')
Putchar (CH );

Return 0;
}
For this program, the reading in the console is invalid.
Before running this program, you must first create a file a.txt with your code.
The generated. EXE file is in the same folder. You can try it.
If you add a statement, the program changes# Include <stdio. h>
# Include <stdlib. h>
Int main ()
{
Char ch;
Freopen ("a.txt", "r", stdin );
Freopen ("B .txt", "W", stdout );

While (CH = getchar ()! = '\ N ')
Putchar (CH );

Return 0;
}
The program will not output anything in the console, but will output all the output to the file "B .txt ".
This B .txt file can be created first, and the program will automatically be created in the same directory as the. exe file.
There are two other problems1. How to determine whether the file is openedIf (freopen ("a.txt", "r", stdin) = NULL) return false;
Or if (freopen ("B .txt", "W", stdout) = NULL) return false;
Indicates that it is not enabled.
2. How to Make the stream return to the consoleIf you do not want to input or output data to a file, add the following sentence:
Freopen ("con", "r", stdin); corresponding Input
Freopen ("con", "W", stdout); corresponding output
Note: because the parameters are all C _ strings, you cannot set the string class pairs in C ++
As a parameter
For example, string STR = "a.txt ";
You cannot write freopen (STR, "r", stdin) like this );
You can first convert the string class to a C _ string, and then use the c_str () function.
The above can be written as freopen (Str. c_str (), "r", stdin );

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.