0

It has been a while since I messed with C/C++, and my memory of the available functions for working with a char* has gone out the window.

I currently use the following code to get the Current Working Directory...

char *path = NULL;
size_t size = 0;

path = _getcwd(path, size);
Msg("Current Working Directory: %s\n", path);

However, I need to take this path, then cut off the last directory, dropping it to the previous directory. It currently stores something like "C:/srcds/orangebox" in path, and I need to drop the last directory from this to get the correct path. In this case, it would be "C:/srcds".

What is the best way to do this with a char*? Please don't suggest to use strings. I know this is immensely easier, but the SDK I use revolves heavily around char* instead, so I try to stick with this for readability.

4
  • 10
    Something wrong with std::string? Apply .c_str() when your API requires a char const*... as late as possible. Really. Especially if you care about "readability" ("legibility"). And what is "C/C++"? Aug 8, 2011 at 14:02
  • 1
    you could always append a '../'
    – Peaches491
    Aug 8, 2011 at 14:02
  • You would only even call them C-strings as opposed to anything else if you were actually working in C++.
    – Puppy
    Aug 8, 2011 at 14:03
  • Actually, I'd use boost::filesystem for this. Aug 8, 2011 at 14:18

5 Answers 5

2

Okay - to keep your life complicated..

  1. use strrchr to find the last \
  2. use memcpy (unless you have strncpy) to copy number of characters to that point.

I should add that 1. will return the last \, so you if your path is like C:/foo/bar/, it will stop at / - so you'll have to do some boundary checks... I did say "to keep your life complicated"...

0
1

It would be immensely easier to just convert it to a string, perform a find_last_of and then convert it back into a char* by calling c_str. But since that's not an option:

Edit: Ohhh I didn't know about strrchar, that would definitely be ideal. You're going to have to just loop through the char string, starting at the last index and working backwards, use strrchar to find a path character (appears to be "/" in your case, but you must ensure this will always be the case) and then string copy (strcpy or one of the related functions) the part of the path you want, up to the index where the beginning of the last folder was found.

Be sure to check for special cases, such as

1) The path returned has only one directory.

2) No path was returned.

3) Any other cases _getcwd might return- check that method's documentation.

0

Hint :

int len= strlen(path);
len--;
while(path[len]!='\\')
{
  len--;
}
path[len]='\0';
0
0

_getcwd is not a standard function, but I fail to find any variant of it where your usage is supported. instead, the behavior for your usage, with null arguments, is either documented as unspecified, or as invoking an error handler.

For dealing wih paths in a general manner, consider using the Boost Filesystem library.

EDIT: I found the following:

As an extension to the POSIX.1-2001 standard, Linux (libc4, libc5, glibc) getcwd() allocates the buffer dynamically using malloc() if buf is NULL on call. In this case, the allocated buffer has the length size unless size is zero, when buf is allocated as big as necessary. It is possible (and, indeed, advisable) to free() the buffers if they have been obtained this way.

Note that the behavior is opposite in Windows, namely guaranteed fail.

cheers & hth.,

0

Something like this?

    char *FullPath = "C:/srcds/orangebox";

    char Buffer[100];

    strcpy(Buffer, FullPath);

    char* Temp = strchr(Buffer, '/');  // find first slash

    if (Temp == NULL)
        ; // handle error

    Temp++;  // skip past slash
    if (Temp == NULL)
        ; // handle error

    Temp = strchr(Temp, '/');  // find second slash

    if (Temp == NULL)
        ; // handle error

    *Temp = 0;  // add string terminator

    puts(Buffer);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.