I have the following code in order to obtain a parent directory from a given path. Note: size_t is a typedef for unsigned int.
/****************************************************
This function takes a full path to a file, and returns
the directory path by returning the string up to the last backslash.
Author: Aashish Bharadwaj
*****************************************************/
_TCHAR* GetDirectoryFromPath(const _TCHAR* path)
{
size_t size = _tcslen(path);
size_t lastBackslash = 0;
for (size_t i = 0; i < size; i++)
{
if (path[i] == '\\')
{
lastBackslash = i;
}
}
_TCHAR* dirPath = new _TCHAR();
size_t i;
for (i = 0; i <= lastBackslash; i++)
{
dirPath[i] = path[i];
}
dirPath[i + 1] = '\0'; //THIS IS VERY NECESSARY! Otherwise, a bunch of garbage is appended to the character array sometimes.
return dirPath;
}
The issue is that sometimes it appends a weird "@" looking symbol to the end of the string that it returns.
I was wondering if anyone knew what this was and why it was doing so.