char *olddir = getcwd(); chdir(newdir); try { do_stuff(); } finally { chdir(olddir); }
Also, getcwd has a size parameter these days, and of course you want to check if the getcwd actually worked.