Monthly Archives: February 2012


C++: Print a string using printf (stdio.h)

In order to print a string in C++ using the printf from the stdio.h library use it as follows:

string Phrase = "Hello!!";
printf("Phrase: %s\n",phrase.c_str());

 


Linux Bash: How to find a string inside C/C++ Source and Header Files

Just issue the following in the folder of the source codes and replace ‘FIND ME’ with the string you want to query:

find -O3 . -regex '.*\.\(c\|cpp\|h\)

You can change the contents of the regular expression to search in different file extensions (file types).  -exec grep 'FIND ME!' -sl '{}' \;

You can change the contents of the regular expression to search in different file extensions (file types). 


PHP: Full Redirect of Arguments in PHP

This is how we forward all arguments from one URL to another you need to put this in a .php file on your server (e.g index.php):

<?php
    header("Status: 301 Moved Permanently");
    header("Location:http://bytefreaks.net/university-of-cyprus".$_SERVER['QUERY_STRING']);
    exit;
?>

Example of Usage:

http://www.cs.ucy.ac.cy?/article

 


Debugging Trick Using Variadic Macros in C and C++

Following you can find a very practical trick that allows you to enable/disable all prints to the screen with as little effort as possible while not dramatically increasing the overall code size to do this.

For C:  Place on the top of your code or on a header file the following:

#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
    #define XDEBUG(...) printf(__VA_ARGS__)
#else
    #define XDEBUG(...)  /**/
#endif

and then inside the code whenever you want to print some debug information do as follows:

XDEBUG("Max Weight %d, Total Trips %d\n", minMax, trips);

For C++:  Place on the top of your code or on a header file the following:

#define ENABLE_DEBUG

#ifdef ENABLE_DEBUG
    #define XDEBUG cout
#else
    #define XDEBUG if(0) cerr
#endif

and then inside the code whenever you want to print some debug information do as follows:

XDEBUG << "We got " << disks << " disks\n";