yes, virginia, you CAN do regular expressions in C/C++

You say you want a regular expression?  Well, you know, you don’t have to install Perl.

There’s a few ways you can go about using regular expressions in your C / C++ code.

  1. The POSIX C regex library.  This presumes of course you are running on a Unix-ish system, e.g. Linux, Mac OSX, iPhone, BSD, etc.
    #include <regex.h>
    regex_t re;
    const char* re_string = "m[Aa]t?ch.*";
    if(regcomp(&re, re_string, REG_NOSUB) != 0){
      std::cerr << "Invalid regular expression" << std::endl;
    if(regexec(&re, "mAchzzzzaaaa!!!!oneone", (size_t)0, NULL, 0) == 0)
      std::cout << "Found a match!" << std::endl;
  2. The Boost Regex POSIX-like API.  This is a C wrapper to the Boost Regex library, which allows you to use the library with POSIX regex calls.
    #include <boost/regex.h>
    // otherwise use just as you would the POSIX lib
  3. The Boost Regex C++ API, straight no chaser.
    #include <boost/regex.hpp>
    boost::regex re("m[Aa]t?ch.*");
    if(boost::regex_match("mAchzzzzaaaa!!!!oneone", re))
      std::cout << "Found a match!" << std::end;

This is but a taste of what’s available.  Peep the POSIX regex API and the Boost Regex Documentation.  Note, Boost Regex is one of only a handful of the Boost packages that needs to be explicitly built and linked with your code.

get info on a file without opening it from C/C++

If you need to get info about a file without opening it, you can use <sys/stat.h> to do so.

I needed a way to test the existence of a file. Version 1 called fopen and tested for NULL, but this also meant some nasty ifs and fclose logic.

Here’s the better way:

#include <iostream>
#include <cerrno>
#include <sys/stat.h>
struct stat fileInfo;
const char* filename = "myFile";
if(stat(filename, &fileInfo) != 0){ //error
  if(errno == ENOENT){
    std::cerr << "File " << myFile << " doesn't exist" << std::endl;
  else{ //any other error
    std::cerr << "Error getting info on file " << filename << ": "
              << strerror(errno) << std::endl;

File existence is but one of the few things you check for.  Just be careful what you check for though, as many of the attributes in struct stat are Unix only.

See stat()’s documentation for more details.

‘auto-release pool’ type behavior in C++ with shared_ptr

With boost’s shared_ptr and standard contaniers, ‘auto-release pool’ (as seen in much Obj-C code) type structures can created in C++.

#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
#include <vector>
class A{...};
typedef shared_ptr<A> A_ptr;
vector<A_ptr> vec_of_a;
for(int i = 0; i < 10; i++){
  A_ptr p(new A());
// do stuff with vector
vec_of_a.clear(); //automagically calls delete on all A_ptrs in vector

To avoid having to call vector::clear(), you can make the vector itself a boost::shared_ptr or boost::scoped_ptr; this will cause the vector itself to be automatically reclaimed, along with its contents.

See Boost’s Smart Pointer Documentation for more info.