user470760
user470760

Reputation:

Using a char* to store the correct file path

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.

Upvotes: 0

Views: 9734

Answers (5)

Steve Wellens
Steve Wellens

Reputation: 20620

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);

Upvotes: 0

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145204

_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.,

Upvotes: 0

Djole
Djole

Reputation: 1145

Hint :

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

Upvotes: 0

Nathan Fig
Nathan Fig

Reputation: 15109

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.

Upvotes: 1

Nim
Nim

Reputation: 33655

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"...

Upvotes: 2

Related Questions