Moving a string from a larger one

This is a discussion on Moving a string from a larger one within the C Programming forums, part of the General Programming Boards category; Another question I have two char variables, path and file_name: Code: char path[256]; char file_name[256]; During the program, it asks ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    15

    Moving a string from a larger one

    Another question

    I have two char variables, path and file_name:

    Code:
    char path[256];
    char file_name[256];
    During the program, it asks the user to input the values for both of the variables. For example, path might contain "C:\Program Files" and file_name would take it a step further: "C:\Program Files\File.txt".

    What I need is to remove the path (in the variable called 'path') from the file_name variable, to get just "File.txt". Can some one please point me in the right direction as how to do this?

    Thanks!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    If you know that the file_name contents exactly duplicates the path + slash + filename

    then
    file_name + strlen(path) + 1 will point to the file name.

    If you are not sure - You can use strrchj to locate the last slash (or backslash) and start looking for the name from the next char
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    15
    I can't use strcch becuase file_name could contain another backslash that I need to keep.

    What I was thinking was to get the ammount of characters in 'path' (30 for example), and then somehow delete the first 30 characters of the 'file_name' string. Is that a better solution? And what's more... how can I do it? I can get the ammount of characters in 'path' thanks to strspn(), but I haven't a clue as how to erase them from the 'file_name' variable.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by notsocks View Post
    I can't use strcch becuase file_name could contain another backslash that I need to keep.
    Due to the context of this post, and since you misspelled the function name, I think it's only fair to assume that you don't know the difference between strchr() and strrchr().

    Unless you're dealing with directories, the latter should work for your usage. The filename will not have any slashes after it, correct?

    Info on both functions:

    http://man.he.net/man3/strchr

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    15
    You assumed correctly: I had no idea. Thanks for pointing that out.

    I AM dealing with directories. How will that make a difference?

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Well it can get kind of hairy if you're expecting whatever tokens you find to be files. fopen() doesn't cope with directories. Just try it yourself and see.

    But, just to illustrate...
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main( void )
    {
      char dir[] = "a/sample/directory";
      char file[] = "a/sample/file.txt";
      char *token;
    
      if ( (token = strrchr( dir, '/' )) != NULL )
      {
        printf( "filepath=%s, *token=%c, token=%s\n", dir, *token, token );
      }
      if ( (token = strrchr( file, '/' )) != NULL )
      {
        printf( "filepath=%s, *token=%c, token=%s\n", file, *token, token );
      }
      return 0;
    }
    See how it can appear to be correct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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