Common debugging methods of ACM

Source: Internet
Author: User

The debugging environment is GDB and the compiler is GCC and G ++. (This is used by the server)
Every time I write an ACM question, I write it well.ProgramThen run and enter the test data given in the question, but debugging is usually not
The problem can be solved once or twice. General questions are okay. Problems with a large number of input such as Web Navigator
It's a headache. Freopen functions provide a very simple solution.

Function Name: freopen
Declaration: file * freopen (const char * path, const char * mode, file * stream );
File: stdio. h
Parameter description:
Path: file name.
Mode: the mode in which the file is opened. It is the same as the mode in fopen (such as R, W.
Stream: a file, usually using standard stream files (stdin, stdout, stderr ).
Returned value: success. A pointer to the file specified by path is returned. Failed. return null. (Generally, its return value is not used)

Function: Simply put, it is to implement redirection. The predefined standard stream files (stdin, stdout, stderr) are directed to the Files specified by path.
For example:
Int main ()
{
// Freopen ("Debug // in.txt", "r", stdin );
While (CIN> ans)
{
// To do .......
}
Cout <Endl;
Return 0;
}
Freopen ("Debug // in.txt", "r", stdin) is used to redirect stdin to the debug // in.txt file, so that CIN or
Data is not extracted from the standard input stream when input with scanf. The input is obtained from the in.txt file. Just paste the input
In.txt makes debugging much easier.

However, there is no completely compatible solution that can restore the standard handle later. There is no way in the C standard library.
The easy way to think of is to re-open the standard console device file, but unfortunately the name of this device file is related to the operating system.

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.