Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Macro definition 1

Status
Not open for further replies.

powerb23

Technical User
Sep 11, 2003
11
0
0
US
Hi,
I would like to define a macro that returns 0 if it cannot open the file and returns 1 if it opens the file.

For example;

#define OPEN(fileHandle) (fileHandle = fopen("blah.dat", "r"))

I would like to use conditional logic but not sure of how to go around it. In this case, the return value of OPEN(fileHandle) would be 0 or 1

Your help will be appreciated.
 
Since you don't parameterise the rest of the macro (eg, the filename), it seems to me to be obfuscating the code more than it it helpful.

But anyway,
Code:
#define OPEN(fileHandle) ( (fileHandle = fopen("blah.dat", "r")) != NULL )

--
 
Thanks for the reply.

Your help was really appreciated.
 
Hi,
I was having problems with how to put a while loop that ignores a line in a macro. I had:

#define IGNORELINE(fileHandle) ( while(c != '\n'){ c = fget(fileHandle);
if(c == EOF) break; })

I however get some errors. I would like to use this using the given macro name.

Your help will be appreciated.
 
Well the blank line in the middle of the #define will do you no good. Each continuation line needs a trailing \, even if there's nothing on it.

> I however get some errors
Since you neither state the errors, or provide a context in which you attempt to use this macro, its hard to say.

But I can't think of anywhere where
Code:
( while
is valid syntax.

Why are you writing everything as macros?
Why not use functions.

--
 
Hi,
it's some homework and i can't figure out how they would do this by passing in only the filename as the parameter. To me, it would seem more logical if i passed in two paramters like this.

#define IGNORELINE(fileHandle,c) ( while(c != '\n'){ c = fget(fileHandle); if(c == EOF) break; })

But i am sure how loops work in a macro. That is something really new to me.


Thanks
 
If you want to code arbitrary code as a single statement, then you do this
Code:
#define SOMETHING do { /* code goes here */ } while(0)

So for you, you could start with
Code:
#define SOMETHING do { int c; /* your code */ } while(0)

--
 
Hi Salem,
Many thanks for your help. I really appreciated your help. I got it to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top