Posted by Loser[TM] on 02:43:00 08-19-2003
I have a string with a path to the file, for example:
char filename[MAX_FNAME] = "C:/test/test.txt"
Now I'd like to remove "C:/test/", so I'd only have a filename. Is there a short way to do this?
[addsig]
Posted by ice911 on 13:45:00 08-20-2003
CString songTitle;
int length = 0;
char slash = 92;
length = m_Path.GetLength();
for(int x = 0; x < length; ++x)
{
if(x < length)
{
songTitle.Empty());
}
if(slash == m_Path[x])
{
++x;
do
{
songTitle += m_Path[x];
++x;
}while(( x != (length)) && (slash != m_Path[x] ));
--x;
}
}
Posted by ice911 on 13:54:00 08-20-2003
Sorry about the code, i new here.
It is in MFC (from a mp3 project) and that is why i use the CString data type instead of the char array.
It works the same way with a few modification like CString's empty() and GetLength().
Basically it works like this:
it goes through the filename string and every backslash it comes to, it records that part of the string from that point. If it comes to another backslash before it reaches the end of the string it will clear what it has recorded and begin recording the string again.
The code shows an example of what to do and will need very little modification from you.
--ice911
(hopes that helps)
Posted by PGuard on 10:02:00 08-24-2003
Try checking the man page on strrchr...
[code]
char *get_filename(const char *absolute_path)
{
char *buf;
// use '\' for windoze code
buf=strrchr(absolute_path,'/');
if(buf)
++buf;
return buf;
}
[code]
And dont free the return pointer.
PGuard