Separating filename from filepath

This is a discussion on Separating filename from filepath within the C++ Programming forums, part of the General Programming Boards category; Here is my problem. I need to keep only the filename from a filepath. For example, if the whole path ...

  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
    801
    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, 02:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 02:43 PM
  3. Replies: 2
    Last Post: 08-29-2008, 06:29 AM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  5. Replies: 3
    Last Post: 01-25-2006, 10:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21