Sign in to follow this  

C++. Being able to see if a linked file exists and then do something about it.

This topic is 3459 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 wondering if this is possible: I have a tool that uses certain files. I want to know if that file exists, and if not, do not use it's functions. Example:
/*main.h*/
#include <foo.h>
#include <faa.h>

bool can_use_foo = false;
bool can_use_faa = false;

#ifdef _FOO_INCLUDED_
can_use_foo = true;
#endif

#ifdef _FAA_INCLUDED_
can_use_faa = true;
#endif

void useFooDoSomething( void )
{

     if( can_use_foo )
     {

          fooDoSomething();

     }
     else
     {

          cout << "Foo was not found. Cannot use Foo's fooDoSomething() function.\n";

     }

}

void useFaaDoSomething( void )
{

     if( can_use_faa )
     {

          faaDoSomethingDifferent();

     }
     else
     {

          cout << "Faa was not found. Cannot use Faa's faaDoSomethingDifferent() function.\n";

     }

}

/*main.cpp*/
#include "main.h"

int main()
{

     useFooDoSomething();
     useFaaDoSomething();
     return 0;

}




So if the linked file wasn't found, instead of giving tons of errors with the compiler, just ignore those foo depended functions. Is this possible? Thanks for your help! ~PCN

Share this post


Link to post
Share on other sites
Yes, with use of the preprocessor -- #define a preprocessor symbol in the 'optional' file, wrap access to that file's functions with #ifdef calls. You'll have to use a stub file when the optional one doesn't exist, otherwise the #include directive will fail.

Macros can alleviate the pain of this somewhat, but it's still to be a disgusting maintainability problem. In other words, this is an extremely bad idea. Why do you think you need this? It's going to make your program brittle and your code ugly.

Share this post


Link to post
Share on other sites
I thought it might be better than making your compiler spit out over 200 errors and the program merely say: "Your file wasn't included" so you can fix it that way. Also, my tool uses more than one of these files, so if one fails ( or if you don't have it ) the program will still run with the exception of that/those particular functions not working.

So would you say it would be a bad idea?

Share this post


Link to post
Share on other sites

This topic is 3459 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