Question about concatenating quotation marks

Discussion in 'C Programming' started by Peng Yu, Oct 1, 2004.

  1. Peng Yu

    Peng Yu Guest

    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##");\
     
    Peng Yu, Oct 1, 2004
    #1
    1. Advertising

  2. Peng Yu

    Michael Mair Guest

    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
     
    Michael Mair, Oct 1, 2004
    #2
    1. Advertising

  3. Peng Yu

    John Bode Guest

    Peng Yu <> wrote in message news:<>...
    > 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");
     
    John Bode, Oct 1, 2004
    #3
  4. On Fri, 01 Oct 2004 09:41:55 -0500, in comp.lang.c , Peng Yu
    <> 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 =---
     
    Mark McIntyre, Oct 2, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Radith

    Using quotation marks

    Radith, Jan 7, 2005, in forum: Java
    Replies:
    1
    Views:
    450
  2. Replies:
    0
    Views:
    610
  3. Tristan Miller
    Replies:
    54
    Views:
    1,474
    ???????? ????? ???????????
    Nov 6, 2003
  4. M. Clift

    String and quotation marks

    M. Clift, Oct 22, 2004, in forum: Python
    Replies:
    2
    Views:
    395
    M. Clift
    Oct 22, 2004
  5. Junior
    Replies:
    4
    Views:
    328
    Dennis Lee Bieber
    Oct 27, 2007
Loading...

Share This Page