PDA

View Full Version : Check if a file or function exist, how? c++



bartbes
January 18th, 2006, 05:03 PM
I have a program written in c++ and I want to inlcude a file if it exists. Currently I am using this code:

#include <fstream>

ifstream backdoorh ( "backdoor.h" );

if ( !backdoorh.is_open() ) {
}
else {
include "backdoor.h "
}

But I get this compilation errors:

aangifte.h:5: fout: expected unqualified-id before ‘if’
aangifte.h:8: fout: expected unqualified-id before ‘else’

What is wrong?:confused: Or does someone knows how to check if a function exists?

adwait
January 18th, 2006, 05:18 PM
with
#ifdef

eg:
#ifdef somefun
#include<something>
#else
#include<something else>

I think.......

LordHunter317
January 18th, 2006, 05:19 PM
If that's your actual code, it's invalid. You can't have a conditional in a function.

Moreover, you can't do conditional preprocessing like that, if that's what you're trying to do. You should probably explain what you're trying to do, first.

jerome bettis
January 19th, 2006, 06:53 AM
for any type of filesystem operations in C++, your best bet is too look at the boost libraries. a few minutes googling for boost filesystem should give you the exact code you need.

LordHunter317
January 19th, 2006, 07:02 AM
for any type of filesystem operations in C++, your best bet is too look at the boost libraries. a few minutes googling for boost filesystem should give you the exact code you need.I think he's trying to conditional preprocessing based on the existance of a file, which boost can't help him with.

jerome bettis
January 19th, 2006, 07:08 AM
yeah not sure what is going on here .... are you using makefile?

thumper
January 19th, 2006, 09:18 AM
How about the makefile checking for the existance of the file and setting a compilation flag then use #ifdef

#ifdef BACKDOOR_EXISTS
#include <backdoor.h>
#endif
GNU make is quite good at this type of thing.

But to answer your original question - like LordHunter317 said - the preprocessor (the bit that handles the #includes) isn't designed to work that way.

bartbes
January 19th, 2006, 03:50 PM
I am using an Makefile but creating now I'm trying to create a .patch file

Lews Therin
January 21st, 2006, 02:09 AM
You appear to be completely misunderstanding the preprocessor. It runs before you compile your program, not at runtime.

bartbes
January 24th, 2006, 12:59 AM
You appear to be completely misunderstanding the preprocessor. It runs before you compile your program, not at runtime.
That's what I realised...