Archive for January 8th, 2008|Daily archive page

Exception Handling in C

http://www.on-time.com/ddj0011.htm
http://sourceforge.net/project/showfiles.php?group_id=4184&package_id=4200
http://en.wikipedia.org/wiki/Exception_handling

“Some programmers use exceptions to handle errors just because their language provides that particular error-handling mechanism… a classic example of programming in a language rather than programming into a language.” – Code Complete 2nd Edition

Write Debug Output to Console Window

http://www.codeguru.com/cpp/v-s/debug/article.php/c1249/

#ifdef _DEBUG
    FILE* __fStdOut = NULL;
    HANDLE __hStdOut = NULL;
#endif

// width and height is the size of console window, if you specify fname,
// the output will also be writton to this file. The file pointer is
// automatically closed when you close the app
void startConsoleWin(int width=80, int height=25, char* fname = NULL);

void startConsoleWin(int width, int height, char* fname)
{
#ifdef _DEBUG
	AllocConsole();
	SetConsoleTitle("Debug Window");
	__hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

	COORD co = {width,height};
	SetConsoleScreenBufferSize(__hStdOut, co);

	if(fname)
		__fStdOut = fopen(fname, "w");
#endif
}

// Use wprintf like TRACE0, TRACE1, ...
// (The arguments are the same as printf)
int wprintf(char *fmt, ...)
{
#ifdef _DEBUG
	char s[300];
	va_list argptr;
	int cnt;

	va_start(argptr, fmt);
	cnt = vsprintf(s, fmt, argptr);
	va_end(argptr);

	DWORD cCharsWritten;

	if(__hStdOut)
		WriteConsole(__hStdOut, s, strlen(s), &cCharsWritten,
				NULL);

	if(__fStdOut)
		fprintf(__fStdOut, s);

	return(cnt);
#else
	return 0;
#endif
}