View printf output information during debugging in MFC

Source: Internet
Author: User
Original article: http://blog.csdn.net/brucezong/article/details/6889974

When debugging the MFC program, we often need to view the output values of variables at specific locations. Or an output identifier is provided when a specific condition is executed.

Generally, there are three methods:

1) Call the trace (lpctstr lpszformat,...) Function

It is very convenient to use the trace function in MFC to print the output results, which is similar to the use of the printf function in the console program. But there are several points

Note: 1. The output of the trace function is under the debug option of the output window. 2. The output is only available when debugging the debug version.

The output is not displayed when the release version is used to debug or run the program.

2) use the afxmessagebox () function to output information.

This function is also commonly used for debugging. It is easy to use and is not described here.

3) direct the standard output to the console you have created

# Include "Io. H"

# Include "fcntl. H"

Void initconsole ()

{

Int nret = 0;

File * FP;

Allocconsole ();

Nret = _ open_osfhandle (long) getstdhandle (std_output_handle), _ o_text );

Fp = _ fdopen (nret, "W ");

* Stdout = * FP;

Setvbuf (stdout, null, _ ionbf, 0 );

}

Call this function at the place where the MFC program is initialized. You can use the console to view the printing information of the printf function.

When debugging the MFC program, we often need to view the output values of variables at specific locations. Or an output identifier is provided when a specific condition is executed.

Generally, there are three methods:

1) Call the trace (lpctstr lpszformat,...) Function

It is very convenient to use the trace function in MFC to print the output results, which is similar to the use of the printf function in the console program. But there are several points

Note: 1. The output of the trace function is under the debug option of the output window. 2. The output is only available when debugging the debug version.

The output is not displayed when the release version is used to debug or run the program.

2) use the afxmessagebox () function to output information.

This function is also commonly used for debugging. It is easy to use and is not described here.

3) direct the standard output to the console you have created

# Include "Io. H"

# Include "fcntl. H"

Void initconsole ()

{

Int nret = 0;

File * FP;

Allocconsole ();

Nret = _ open_osfhandle (long) getstdhandle (std_output_handle), _ o_text );

Fp = _ fdopen (nret, "W ");

* Stdout = * FP;

Setvbuf (stdout, null, _ ionbf, 0 );

}

Call this function at the place where the MFC program is initialized. You can use the console to view the printing information of the printf function.

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.