Sign in to follow this  

__FILE__ w/o fullpath

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i'm finishing the debuging tool, inspired by the article "uisng xml technologies for enhanced log files", but i'm curious with the __FILE__ macro. in the pictures there, only the filename (CKernel.cpp) was displayed, but when i use the __FILE__ macro the whole path is printed. can you tell me how i can switch between the fullpath and just the filename? i didn't find anything usefull in the msdn...

Share this post


Link to post
Share on other sites
As far as I know, the exact value of __FILE__ is defined by the compiler. mmgr always trims the path from the filename when logging the file, and I always do that myself. As far as I know, there is no way to get just the filename.

Share this post


Link to post
Share on other sites
I assume you're using some flavour of Visual Studio. In Debug mode, Visual Studio substitutes the __FILE__ macro for the source file's full path and name. In Release mode, __FILE__ is substituted for just the source file's name. I assume that other IDEs and compilers do the same. If you think about it, it makes sense: a program built in Release mode will probably be running on a completely different machine to the one it was compiled on; including paths with the filenames would be meaningless. In short, the compiler decides what it substitutes for __FILE__, and there's not a damn thing you can do about it [grin].

EDIT: But come to think of it, macros are a pre-processor step, so if you really just wanted the filename, you could send the string substituted for the __FILE__ macro to a function which extracts the filename from a given string: GetFilename(__FILE__). Should be simple enough to code.

Share this post


Link to post
Share on other sites
If all else fails, you can just extract the needed portion of the file path. Getting the file name from the full path should be nothing more than this code: -



// create the string
string strPath = "c:\\files\\directory\\file.cpp";
string strFName = "";

// loop through the string and get the filename from the end of the string
int nSize = strPath.size();
for ( int i = nSize - 1; i >= 0; i-- )
{
if ( strPath[ i ] == '\\' )
break;
strFName = string( strPath[ i ] ) + strFName;
} // end for

// do some logging with strPath
// ...





There are more than likely much more efficient methods of doing what I just did, but I'm sure you get the idea.

Share this post


Link to post
Share on other sites
thanks for the replies. isn't there a pre written getfilename function? i'm not shure, but i think there is one, i just don't know wich header i have to include.
i hate the new msdn, it tells me everything about that [...] function, but nothing about the required header file *rolleyes* i also cannot find something 'bout it on the web

btw: i tried the release built, but not all __FILE__ macros are replaced. some are, some are not. i don't understand it...

[Edited by - SiS-Shadowman on March 26, 2006 5:16:07 PM]

Share this post


Link to post
Share on other sites
I just finished up my logger, and had the same question. The best thing I found was this:

std::string str = __FILE__;
int pos = str.find_last_of('\\');
str = str.substr(pos+1);


here is the link to the compiler settings, if you can find how to do it in VS 7.1 I would love to here how (I could not find the stupid option)

Share this post


Link to post
Share on other sites
Quote:
Original post by SiS-Shadowman
i'm finishing the debuging tool, inspired by the article "uisng xml technologies for enhanced log files"


I took a look at google but I couldn't find this article, could you post a link?

Share this post


Link to post
Share on other sites

This topic is 4278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this