>> Still doesn't explain my current error though, which is what I'm most interested in at the moment...
Did fixing the substr call help?
>> tempdirectory.erase(0,tempdirectory.length());
BTW, this line does nothing important. The variable is about to go out of scope anyway, and if you wanted to clear a string you should use clear().