Question about concatenating quotation marks

P

Peng Yu

I'm going to do something like this

#define INSPECT( FILENAME,) \
ofstream FILENAME("FILENAME.out");\

For example, I want "INSPECT(abc)" open a file with name "abc.out",
the file object's name is "abc".

I tried the following code, but it still doesn't work.
#define INSPECT( FILENAME,) \
ofstream FILENAME("##FILENAME.out##");\
 
M

Michael Mair

Hiho,
I'm going to do something like this

#define INSPECT( FILENAME,) \
ofstream FILENAME("FILENAME.out");\

For example, I want "INSPECT(abc)" open a file with name "abc.out",
the file object's name is "abc".

I tried the following code, but it still doesn't work.
#define INSPECT( FILENAME,) \
ofstream FILENAME("##FILENAME.out##");\

Last time I checked, ofstream was not in the C language...
So, this code will not compile, but the C preprocessor
will do what you want it to:

#define STRINGIZE(s) # s
#define XSTR(s) STRINGIZE(s)

#define INSPECT(file) \
ofstream (file)(XSTR(file) ".out")

int main (void)
{
INSPECT(test);

return 0;
}

Note: the string part becomes
"test" ".out"
which will be seen as "test.out" by the compiler.
The preprocessor output of
INSPECT(test);
is
ofstream (test)("test" ".out");
If the additional set of parentheses around test
gives you problems, then leave them out after understanding
what that means.


--Michael
 
J

John Bode

Peng Yu said:
I'm going to do something like this

#define INSPECT( FILENAME,) \
ofstream FILENAME("FILENAME.out");\

For example, I want "INSPECT(abc)" open a file with name "abc.out",
the file object's name is "abc".

I tried the following code, but it still doesn't work.
#define INSPECT( FILENAME,) \
ofstream FILENAME("##FILENAME.out##");\

Try

#define INSPECT(FILENAME) \
ofstream FILENAME(#FILENAME ".out")

During expansion, #FILENAME will be replaced with the value of
FILENAME surrounded by double quotes, which will be concatenated with
the ".out" string.

Example:

INSPECT(abc);

should expand to

ofstream abc("abc.out");
 
M

Mark McIntyre

I'm going to do something like this

You need to explain what you're trying to do. The below doesn't make much
sense
#define INSPECT( FILENAME,) \
ofstream FILENAME("FILENAME.out");\

ofstream looks like C++. You're in the wrong group.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top