0

I use the __FILE__ macro for error messages. However, sometimes the path comes back as E:\x\y\/z.ext. It does this for specific files.

For example, E:\programming\v2\wwwindowclass.h comes back as E:\programming\v2\/wwwindowclass.h and E:\programming\v2\test.cpp comes back as E:\programming\v2\test.cpp. In fact, the only file in the directory that works seems to be test.cpp.

To work around this, I used jmucchiello's answer to this question to replace any occurrence of "/" with "\". This worked fine, and the displayed path changed to a normal one.

The problem was when I tried it on Windows 7 (after using XP). The string came up as (null) after calling the function.

Along with this, I sometimes get some seemingly random error 2: File not found errors. I'm unsure of whether this is related at all, but if there's an explanation, it would be nice to hear.

I've tried to find why __FILE__ would be returning the wrong string, but to no avail. I'm using GNU g++ 4.6.1. I'm not actually sure yet if the paths that were wrong in XP were wrong in Windows 7 too. Any insight is appreciated.

Community
  • 1
  • 1
chris
  • 60,560
  • 13
  • 143
  • 205
  • 1
    It is not returning the wrong string, it is just that this is one of the many implementation defined ways to deal with this. Often stuff needs \\ to escape \ properly, read the documentation for your OS on how it expects path names for the functionality it needs – PlasmaHH Mar 09 '12 at 14:54
  • But the true path being returned is `E:\\programming\\v2\\/wwwindowclass.h`. I don't see the use for that extra `/`. And why would it do that for most files, but not `test.cpp`? – chris Mar 09 '12 at 15:03
  • @chris: Typically, the compiler does so when *you* pass `#include "v2/wwwindowclass.h"` to the compiler. Since every file has its own include statements, you can (but shouldn't) mix the two styles. – MSalters Mar 09 '12 at 15:27
  • Ahhh, I see exactly what you mean now. My compiler just puts `/file.ext` on the end. I include my files like `#include "wwwindowclass.h"` and by default it puts in that slash. If I say `#include ".\wwwindowclass.h"` it shows up as `a\b\c\/.\wwwindowclass.h`. Would you make the compiler adding that slash onto the include into an answer? – chris Mar 09 '12 at 18:51

2 Answers2

1

The function in the linked question appears to return NULL if there are no changes to make. Probably Windows 7 doesn't suffer from the \/ problem (in some cases).

ams
  • 24,923
  • 4
  • 54
  • 75
0

As per MSalters's comment:

Typically, the compiler does so when you pass #include "v2/wwwindowclass.h" to the compiler. 
Since every file has its own include statements, you can (but shouldn't) mix the two styles.

This was the case. My compiler automatically adds a forward slash.

chris
  • 60,560
  • 13
  • 143
  • 205