Skip to main content
GameDev.net gamedev.net
🔒 Locked

Truncate File Path

Started by fgtbear Mar 19, 2003 at 8:31 PM 5 replies 300+ views
Original Post
fgtbear
fgtbear
If I have a char thats holds a file path (ie: c:\blah\programs\whatever\gamedev.exe), how would I go about truncating it to just "gamedev.exe".
falkone
falkone
I''d move through the characters (starting at the last char) and look for the ''\'' character.. then take whatever is after that and do whatever with it..
Disclaimer: "I am in no way qualified to present advice n any topic concerning anything and can not be held responsible for any damages that my advice may incurr (due to neither my negligence nor yours)" o
n0ob
n0ob
well, you could grab the length of the entire string, start at the end and copy characters backwards into another array until you reach a /. Then you just reverse your temporary array and, bingo, you have the file!

c:\blah\programs\whatever\gamedev.exe -->
exe.vedemag -->
gamedev.exe!

Kings of Chaos

ps: yah, what falkone said ^_^

[edited by - n0ob on March 19, 2003 9:37:08 PM]
fgtbear
fgtbear
Im new to string modification. What would I use to read the string backwards?
zealouselixir
zealouselixir

bool GetFileName(char *filePath, char *fileName)
// IN: filePath is a valid string containing the complete path.
// OUT: fileName is a char array containing at least as many characters as the file name, plus one.
// OUT: returns true if operation succeeded, false otherwise.
{
char *temp = filePath;

unsigned long index = strlen(temp);
bool isValid = false;
while(index >= 0)
{
if(temp[index] == '/' || temp[index] == '\\')
{
isValid = true;
break;
}
--index;
}

if(!isValid)
return false;

index ++;

unsigned long index2 = 0;
while(temp[index])
fileName[index2++] = temp[index++];
fileName[index2] = 0;

return true;
}

That's just off the top. I'm sure it's not the most efficient way, and someone will probably come along and suggest something based on the STL, but AFAIK, that works (with perhaps minor modifications).

Later,
ZE.



//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links


[edited by - zealouselixir on March 19, 2003 10:01:16 PM]
[twitter]warrenm[/twitter]
fgtbear
fgtbear
Doesnt that just return if the filename is valid? I need it to truncate the filepath so it just leaves the actual file to a a char.

Example:
char *filepath="c:\blah\blah2\blah3\gamedev.exe"
filepath="gamedev.exe" (after function)
zealouselixir
zealouselixir
No, it doesn''t just return if the filename is valid. It copies the filename from the filepath into a new string. To get the filename back in the original string, just

sprintf(filePath, fileName);
[twitter]warrenm[/twitter]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.