Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Question about concatenating quotation marks (http://www.velocityreviews.com/forums/t435354-question-about-concatenating-quotation-marks.html)

Peng Yu 10-01-2004 02:41 PM

Question about concatenating quotation marks
 
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##");\


Michael Mair 10-01-2004 03:05 PM

Re: Question about concatenating quotation marks
 
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


John Bode 10-01-2004 09:05 PM

Re: Question about concatenating quotation marks
 
Peng Yu <pengyu.ut@gmail.com> wrote in message news:<fsqql09hrfj6lb6emt4jdno1tcbakrfut9@4ax.com>. ..
> 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");

Mark McIntyre 10-02-2004 08:08 PM

Re: Question about concatenating quotation marks
 
On Fri, 01 Oct 2004 09:41:55 -0500, in comp.lang.c , Peng Yu
<pengyu.ut@gmail.com> wrote:

>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.


--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>


----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---


All times are GMT. The time now is 07:55 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.