Thread: Copying the end of a string

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    216

    Copying the end of a string

    Hello,

    So I have a string with a full filepath. However, I only want the immediate filename, without the full path. How can I remove the other bits or even just copy the part I want into another variable. Here is how it should work from what I can gather.

    Code:
    char file[] = "C:\\Users\\Administer\\Desktop\\doc.txt";
    
    // find last occurence of '\'
    char *location = strrchr(file, '\');
    
    // last occurence is now in location
    So now how can I copy the rest of the string AFTER the last '\'? What am I missing though, or is there an easier approach?

    In the end I would like to convert this

    Code:
    char file[] = "C:\\Users\\Administer\\Desktop\\doc.txt";
    /* to this */
    char newName[] = "doc.txt";
    Last edited by binks; 06-12-2012 at 07:31 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Try this test program:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char file[] = "C:\\Users\\Administer\\Desktop\\doc.txt";
    
        char *location = strrchr(file, '\\');
    
        printf("%s\n", location + 1);
    
        return 0;
    }
    Do you see how to proceed from here?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    I suppose you could always start at the NULL and work your way back to the first occurance of the slash and save yourself the function call (not that there's anything wrong with calling strrchr()).
    Jason Deckard

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Deckard
    I suppose you could always start at the NULL and work your way back to the first occurance of the slash and save yourself the function call (not that there's anything wrong with calling strrchr()).
    The way I see it, if the function call overhead is not causing some kind of performance problem and if depending on the C standard library is perfectly fine then that would be rather pointless.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    It's just another way to skin the cat; it's not criticism.
    Jason Deckard

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Deckard View Post
    I suppose you could always start at the NULL and work your way back to the first occurance of the slash and save yourself the function call (not that there's anything wrong with calling strrchr()).
    If you already know where the null terminator is, you ought to know where the last backslash is too, so you can just as easily hardcode an offset into the path to strip off everything but the file name.

    More likely you don't know where the null terminator is to begin with, so you would have to search for it (or use strlen) before you can search backwards for the backslash, but by that point you might as well just call strrchr to do the searching for you. Your C implementation most likely has a fast (and well-tested) strrchr which would be difficult to match in performance.

    Summary: don't reinvent the wheel.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Quote Originally Posted by laserlight View Post
    Try this test program:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char file[] = "C:\\Users\\Administer\\Desktop\\doc.txt";
    
        char *location = strrchr(file, '\\');
    
        printf("%s\n", location + 1);
    
        return 0;
    }
    Do you see how to proceed from here?
    Yes, Ok I see it now. I tested it using strcpy as well, and it works. So now I know I can treat location + 1 as a string.

    Thanks for the help, I can take it from here

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ok, I have a problem. What is wrong here?

    Code:
    char newNames[numFiles][256];
    char fileNames[][256] = {"Test1.zip", "Test2.rar"};
    int numfiles = 2;
    
          for(i = 0; i < numFiles; i++)
          {
                char *location = strrchr(fileNames[i], '\\');
                strcpy(newNames[i], location + 1); // crashes here
          }

  9. #9
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by binks View Post
    Ok, I have a problem. What is wrong here?

    Code:
    char newNames[numFiles][256];
    char fileNames[][256] = {"Test1.zip", "Test2.rar"};
    int numfiles = 2;
    
          for(i = 0; i < numFiles; i++)
          {
                char *location = strrchr(fileNames[i], '\\');
                strcpy(newNames[i], location + 1); // crashes here
          }
    If the string you pass to strrchr() doesn't contain the character you want it to look for, strrchr() returns a null pointer. You then pass this null pointer to strcpy(). The strcpy() function cannot deal with a null pointer, especially if you add 1 to that null pointer (which means it's not really a null pointer anymore ... it's just a bogus pointer).

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    So after I call strrchr, I should check to see if:

    Code:
    if(location == NULL)
    Or do I have to go

    Code:
    strcmp(location, NULL);

  11. #11
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    The former. If location is null then strrchr() has failed to find a backslash in the path name, which means ... ah screw it, I'll just show you what I mean:

    Code:
    char newNames[numFiles][256];
    char fileNames[][256] = {"Test1.zip", "Test2.rar"};
    int numfiles = 2;
     
          for(i = 0; i < numFiles; i++)
          {
                char *location = strrchr(fileNames[i], '\\');
                if ( location )
                {
                    strcpy(newNames[i], location + 1);
                }
                else
                {
                    // path contains no backslash
                    strcpy(newNames[i], fileNames[i]);
                }
          }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-10-2011, 05:25 AM
  2. Copying a string, into a string array.
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-31-2006, 05:19 PM
  3. String copying
    By bobthebullet990 in forum C Programming
    Replies: 12
    Last Post: 11-28-2005, 09:31 AM
  4. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  5. Copying one string from s2[] to s1[]
    By steve_demaio in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 05:01 PM