473,385 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to use #ifndef #define, etc, in a macro definition

I want to define a macro

#define FILEWR(FILENAME)

which can be expanded to

#ifndef OUTFILE
#define OUTFILE
ofstream out;
#endif
out.open(FILENAME);
out << "A" << endl;
out.close();
Do you have any idea how to do this? Thanks!

Peng
Jul 22 '05 #1
6 3129

"Peng Yu" <pe*******@gmail.com> wrote in message
news:nf********************************@4ax.com...
I want to define a macro

#define FILEWR(FILENAME)

which can be expanded to

#ifndef OUTFILE
#define OUTFILE
ofstream out;
#endif
out.open(FILENAME);
out << "A" << endl;
out.close();
Do you have any idea how to do this? Thanks!


Impossible, you cannot use pre-processing directives in a macro.

john
Jul 22 '05 #2
Peng Yu posted:
I want to define a macro

#define FILEWR(FILENAME)

inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE

std::ofstream out;

#define OUTFILE
#endif

out.open(filename);
out << "A\n";
out.close();
}
Here's how it would work:

If you're going to define a global object, you must do it as so:

ofstream out;
#define OUTFILE
If the global object is already defined, it will be used by the function.
If the global object is *not* already defined, an object called "out", which
will be a local object of the function and which will of automatic duration
(ie. it will be destroyed at the end of the function), will be defined and
used by the function.
Alternatively, you could make it static:

inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE

static std::ofstream out;

#define OUTFILE
#endif

out.open(filename);
out << "A\n";
out.close();
}
Note that I don't know what you're doing... and I don't want to know!
-JKop
Jul 22 '05 #3
> Alternatively, you could make it static:

inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE

static std::ofstream out;

#define OUTFILE
#endif

out.open(filename);
out << "A\n";
out.close();
}


In this case, the #ifndef ... #endif is not needed since static objects are
declared only once within their scope anyway.
Jul 22 '05 #4
Method Man posted:
Alternatively, you could make it static:

inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE

static std::ofstream out;

#define OUTFILE
#endif

out.open(filename);
out << "A\n";
out.close();
}


In this case, the #ifndef ... #endif is not needed since static objects
are declared only once within their scope anyway.


....read my last post. Pay particular attention to the part that mentions the
global variable.
-JKop
Jul 22 '05 #5
Peng Yu <pe*******@gmail.com> wrote:

I want to define a macro

#define FILEWR(FILENAME)

which can be expanded to

#ifndef OUTFILE
#define OUTFILE
ofstream out;
#endif
out.open(FILENAME);
out << "A" << endl;
out.close();


Have you considered:

#define FILEWR(FILENAME) \
{ ofstream out; out.open(FILENAME); out << "A\n"; }

(the stream is automatically closed and flushed when it goes out of
scope), or better (?) is

#define FILEWR(FILENAME) (ofstream(FILENAME) << "A\n")

(BTW this is stupid because of no error-checking or exception
handling; you should use a function)
Jul 22 '05 #6

"JKop" <NU**@NULL.NULL> wrote in message
news:QJ*******************@news.indigo.ie...
Method Man posted:
Alternatively, you could make it static:

inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE

static std::ofstream out;

#define OUTFILE
#endif

out.open(filename);
out << "A\n";
out.close();
}

In this case, the #ifndef ... #endif is not needed since static objects
are declared only once within their scope anyway.


...read my last post. Pay particular attention to the part that mentions

the global variable.


Sorry I misread your post; I didn't know you were referring back to the
global object.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Evan | last post by:
Is there any standard for when to use #if !defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD? Aside from being able to combine multiple things into an #if, is there any...
3
by: prettysmurfed | last post by:
Hi all I have this, probably stupid question, how to avoid multiple definitions when a header file is included more than once. I thought, when you wrote the header-file and used the...
5
by: DrUg13 | last post by:
#ifndef HEADER_H #define HEADER_H blah blal #endif So this tells the compiler That if its defined do not define it again. Could someone help me understand this. Does this mean that if I...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
13
by: marcwentink | last post by:
Dear people, The code below is compiling: #define BUF_SZ 16383 ..... strcat(ConnectString, "Content-Length: BUF_SZ\n"); but it does not work since it give me:
11
by: Frank Silvermann | last post by:
#define q p I must admit to a most elementary confusion. I have only ever used #define to, in point of fact, #redefine . The above is standard. Is the following: #define...
19
by: Sensei | last post by:
Hi! I'm concerned about the legality of such a definition: #define funcX funcY where funcX belongs to the *standard* C functions. Is it legal to do this? The standard says "any function...
4
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
4
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.