Replace _last_ slash in directory name string

This is a discussion on Replace _last_ slash in directory name string within the C Programming forums, part of the General Programming Boards category; Hey guys, Hope anyone can help me with my little problem, below. I've got this string: ./vehicle_database/Ford/Mondeo 2006 Handbrake (! ...

  1. #1
    1ST ╗ R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    Replace _last_ slash in directory name string

    Hey guys,

    Hope anyone can help me with my little problem, below.

    I've got this string:
    ./vehicle_database/Ford/Mondeo 2006 Handbrake

    (! this is an example, there are many similar strings needed in my program, e.g. ./vehicle_database/Audi/A6 3LV6 Brakelights )


    And I want to place a file "one folder up", so in:
    ./vehicle_database/Ford

    My idea was to replace the last forward slash in the first string (after Ford) with a '\0' but I can't get it to work.
    I know about strchr, but that only looks for the _first_ time the specified character is in the string.

    Code:
          if((p = strchr(data_dir, '/')) != NULL) // search for '/'
          {
             *p = '\0'; // replace / with \0
          }
    Am I on the right way? Can I do it with strchr and a special trick, or am I completely wrong? Is there a way for doing this anyway? (of course there is! )

    Hope you can help, thanks in advance,


    René
    Last edited by rkooij; 08-22-2006 at 04:23 AM.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If "id_file_array" is an array of characters, then leave off the "[cnt2]". If not, and it's a 2D array, or an array of pointers to characters, then you'r doing it right.

    How is 'id_file_array' declared?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    1ST ╗ R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by quzah
    If "id_file_array" is an array of characters, then leave off the "[cnt2]". If not, and it's a 2D array, or an array of pointers to characters, then you'r doing it right.

    How is 'id_file_array' declared?


    Quzah.
    I'm sorry, I just edited my first post. I took that example from another part of the code. There's no id_file_array[cnt2] involved in this problem. (it's just data_dir, which btw is declared as char data_dir[256])
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Well, without using any string functions, you could try something like this, I suppose.
    Code:
    #include <stdio.h>
    
    int main(void) {
        char pathname[256] = "./vehicle_database/Ford/Mondeo 2006 Handbrake";
        char *charFind = NULL;
        
        printf("%s\n", pathname);
        
        int i;
        for(i = 0; pathname[i] != '\0'; i++)
          if(pathname[i] == '/')
             charFind = &(pathname[i]);
          
        *charFind = '\0';
        
        printf("%s", pathname);
    
        return 0;
    }
    I hope there is no real mistakes in there, I'm kinda tired. I did skimp a bit on error checking, though, so you might want to be more careful in a real implementation. You'll likely find a better method using strtok or some other functions, but this should give you an idea of what you *can* do.

    EDIT: You might as well use OnionKnight's solution as I need to spruce up on my ANSI C syntax.
    Last edited by SlyMaelstrom; 08-22-2006 at 05:14 AM.
    Sent from my iPad«

  5. #5
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Quote Originally Posted by rkooij
    Code:
          if((p = strchr(data_dir, '/')) != NULL) // search for '/'
          {
             *p = '\0'; // replace / with \0
          }
    Am I on the right way? Can I do it with strchr and a special trick, or am I completely wrong? Is there a way for doing this anyway? (of course there is! )

    Hope you can help, thanks in advance,


    Ren&#233;
    Replace strchr with strrchr and you've got it!

  6. #6
    1ST ╗ R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by OnionKnight
    Replace strchr with strrchr and you've got it!
    Heyyy that's perfect, thanks!!

    @SlyMaelstrom: what does this part do:
    Code:
    charFind = &(pathname[i]);
    ?
    I've never seen = &() before.
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by rkooij
    Heyyy that's perfect, thanks!!

    @SlyMaelstrom: what does this part do:
    Code:
    charFind = &(pathname[i]);
    ?
    I've never seen = &() before.
    It's not one operator, the parenthesis just ensures that the subscript operator resolves first, which I'm pretty sure it does anyway, and then I just take the address of that value.
    Sent from my iPad«

  8. #8
    1ST ╗ R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154

    Red face

    Quote Originally Posted by SlyMaelstrom
    ... the parenthesis just ensures that the subscript operator resolves first...
    Wheeee , I dont understand a WORD of what you just said, haha!
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by rkooij
    Wheeee , I dont understand a WORD of what you just said, haha!
    Alrighty... in &(var[i]) there are three operators. The "address of" operator (&), the "grouping" operator (()), and the "subscript" operator ([]). The grouping operator has the highest precedence of all operators (I believe the subscript operator has the second highest, or possibly the same precedence, but I wanted to be safe) so in order to ensure that the subscript operator is resolved (used/called/happens) first, I grouped it in with the variable and left the third operator & outside to be resolved last. Get it?
    Last edited by SlyMaelstrom; 08-22-2006 at 05:36 AM.
    Sent from my iPad«

  10. #10
    1ST ╗ R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Quote Originally Posted by SlyMaelstrom
    Alrighty... in &(var[i]) there are three operators. The "address of" operator (&), the "grouping" operator (()), and the "subscript" operator ([]). The grouping operator has the highest precedence of all operators (I believe the subscript operator has the second highest, or possibly the same precedence, but I wanted to be safe) so in order to ensure that the subscript operator is resolved (used/called/happens) first, I grouped it in with the variable and left the third operator & outside to be resolved last. Get it?
    Yeah, I get it now! Thanks for explaining inspite of being tired

    You're sure you're not a compiler?
    http://www.f1rstracing.nl/
    OS: Windows XP
    Compiler: Dev-C++

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by rkooij
    Yeah, I get it now! Thanks for explaining inspite of being tired

    You're sure you're not a compiler?
    Yeah, I'm pretty positive that I could never be mistaken for a C compiler. Though with Miracle C out there, who really knows?
    Sent from my iPad«

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by OnionKnight
    Replace strchr with strrchr and you've got it!
    Bah. When I read it I read it as strrchr. :P Good catch.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. string replace and erase/insert
    By divineleft in forum C++ Programming
    Replies: 3
    Last Post: 07-05-2006, 05:02 AM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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