共用方式為


_CrtSetReportFile

 

The latest version of this topic can be found at _CrtSetReportFile.

After you use _CrtSetReportMode to specify _CRTDBG_MODE_FILE, you can specify the file handle to receive the message text. _CrtSetReportFile is also used by _CrtDbgReport, _CrtDbgReportW to specify the destination of text (debug version only).

Syntax

_HFILE _CrtSetReportFile(   
   int reportType,  
   _HFILE reportFile   
);  

Parameters

reportType
Report type: _CRT_WARN, _CRT_ERROR, and _CRT_ASSERT.

reportFile
New report file for reportType.

Return Value

On successful completion, _CrtSetReportFile returns the previous report file defined for the report type specified in reportType. If an invalid value is passed in for reportType, this function invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, errno is set to EINVAL and the function returns _CRTDBG_HFILE_ERROR. For more information, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

_CrtSetReportFile is used with the _CrtSetReportMode function to define the destination or destinations for a specific report type generated by _CrtDbgReport. When _CrtSetReportMode has been called to assign the _CRTDBG_MODE_FILE reporting mode for a specific report type, _CrtSetReportFile should then be called to define the specific file or stream to use as the destination. When _DEBUG is not defined, calls to _CrtSetReportFile are removed during preprocessing.

The following table shows a list of the available choices for reportFile and the resulting behavior of _CrtDbgReport. These options are defined as bit flags in Crtdbg.h.

file handle
A handle to the file that will be the destination for messages. No attempt is made to verify the validity of the handle. You must open and close the handle to the file. For example:

HANDLE hLogFile;  
hLogFile = CreateFile("c:\\log.txt", GENERIC_WRITE,   
   FILE_SHARE_WRITE, NULL, CREATE_ALWAYS,   
   FILE_ATTRIBUTE_NORMAL, NULL);  
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);  
_CrtSetReportFile(_CRT_WARN, hLogFile);  
  
_RPT0(_CRT_WARN,"file message\n");  
CloseHandle(hLogFile);  

_CRTDBG_FILE_STDERR
Writes message to stderr, which can be redirected as follows:

freopen( "c:\\log2.txt", "w", stderr);  
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);  
_CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);  
  
_RPT0(_CRT_ERROR,"1st message\n");  

_CRTDBG_FILE_STDOUT
Writes message to stdout, which you can redirect.

_CRTDBG_REPORT_FILE
Returns the current report mode.

The report file used by each report type can be separately controlled. For example, it is possible to specify that a reportType of _CRT_ERROR be reported to stderr, while a reportType of _CRT_ASSERT be reported to a user-defined file handle or stream.

Requirements

Routine Required header Optional header
_CrtSetReportFile <crtdbg.h> <errno.h>

The console is not supported in Windows 8.x Store apps. The standard stream handles that are associated with the console—stdin, stdout, and stderr—must be redirected before C run-time functions can use them in Windows 8.x Store apps. For more compatibility information, see Compatibility.

Libraries: Debug versions of CRT Library Features only.

.NET Framework Equivalent

Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.

See Also

Debug Routines