concatenating Directories
Author |
Message |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Sat Aug 22, 2009 10:24 pm Post subject: concatenating Directories |
|
|
If you have the directory 'a' with the file 'b' in it, it would be 'a/b' on Unix, but 'a\b' on Windows. In Pyhton, this could be solved by using os.path.join('a', 'b'), and this is what I came up for in C:
c: | #define PATH_JOIN(a,b) "a/b" /* Unix */
#define PATH_JOIN(a,b) "a\\b" /* Windows */
|
This works for what I currently need it for, however it has the downside that it only works with two constants. You can't use user input in any way, or any other variables.
I also tried
c: | #define SLASH /
"aSLASHb" |
However, the compiler (at least GCC) doesn't expand macros inside strings, which makes sense.
Is there a better way to join directories? |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Sat Aug 22, 2009 11:11 pm Post subject: RE:concatenating Directories |
|
|
People on the internets say in C you can just use a forward slash for everything and it's supposed to work properly on every platform.
Are you just trying to open a file or are you doing something else?
Source: here. |
|
|
|
|
![](images/spacer.gif) |
DtY
![](http://compsci.ca/v3/uploads/user_avatars/8576159234be48b7a8b0e8.png)
|
Posted: Sun Aug 23, 2009 11:20 am Post subject: RE:concatenating Directories |
|
|
Thank you. That looks like what I need
I'll be using fopen(), and SDL_loadBMP(), which I assume uses fopen() |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Sun Aug 23, 2009 5:16 pm Post subject: Re: concatenating Directories |
|
|
DtY @ Sat Aug 22, 2009 10:24 pm wrote: c: | #define PATH_JOIN(a,b) "a/b" /* Unix */
#define PATH_JOIN(a,b) "a\\b" /* Windows */
| ...
However, the compiler (at least GCC) doesn't expand macros inside strings, which makes sense.
It does not make sense for it to replace supposed macro parameters inside a string either. |
|
|
|
|
![](images/spacer.gif) |
btiffin
![](http://compsci.ca/v3/uploads/user_avatars/189169540547b535e50e4a7.jpg)
|
Posted: Sun Aug 23, 2009 10:52 pm Post subject: Re: concatenating Directories |
|
|
In the Just in case you ever need to know department
http://en.wikipedia.org/wiki/C_preprocessor#Quoting_macro_arguments
The #var trick can be used with auto literal concat in C to let you build up literals in code with the preprocessor
Something like
code: |
#define path(a,b) #a "/" #b
#define path(a,b) #a "\\" #b
|
Untested, and cpp can pass on data dependent quoting weirdness And, it'll be far better to follow previous advice and not mucky muck with platform path issues unless you really really have to.
Cheers |
|
|
|
|
![](images/spacer.gif) |
|
|