Thread: Separating filename from filepath

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Separating filename from filepath

    Here is my problem. I need to keep only the filename from a filepath. For example, if the whole path is like
    LPCTSTR str = "C:\\Test\\pic.bmp";

    How do I retrieve just the pic.bmp into a string?
    Thanks
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Funniest man in this seat minesweeper's Avatar
    Join Date
    Mar 2002
    Posts
    798
    Could you not iterate from the end of the string, back until you reach the first '\' you come across and then iterate forward again copying the data into another string?

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Yea, i did that right after posting
    Thanks anyway!
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Why iterate at all? The wonderful functions of <cstring> can probably do it better and in less space :-)
    Code:
    char *p;
    char buff[1024];
    if ((p = strrchr(str, '\\\')) != 0)
    {
      strcpy(buff, p+1);
      // Now buff has "pic.bmp"
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. Replies: 2
    Last Post: 08-29-2008, 05:29 AM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  5. Replies: 3
    Last Post: 01-25-2006, 10:30 PM