Copying the end of a string

This is a discussion on Copying the end of a string within the C Programming forums, part of the General Programming Boards category; Hello, So I have a string with a full filepath. However, I only want the immediate filename, without the full ...

  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
    21,748
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    B26354 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
    21,748
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    B26354 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
    383
    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
    249
    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
    249
    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, 08:31 AM
  4. Replies: 1
    Last Post: 10-31-2005, 10: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

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