Thread: Problem with a text macro.

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    58

    Problem with a text macro.

    Hi,

    Just found out about preprocessor macros, and was trying to create a popen command that would include a changing directory.

    I tried the following:
    Code:
    #define COMMAND(dir) "find " #dir " -type f -printf '%T@ %t %p\\n' | sort -k 1 -nr | rev | cut -d' ' -f1 | rev"
    And it does what I need with the following:
    Code:
    printf("%s\n", COMMAND("/home/user/directory"));
    But I'm now trying to use a '#define' as the argument for the macro, but it just uses the NAME rather than the value:
    Code:
    printf("%s\n", COMMAND(DIRECTORY));
    Prints:
    Code:
    find DIRECTORY -type f -printf '%T@ %t %p\n' | sort -k 1 -nr | rev | cut -d' ' -f1 | rev
    Instead of:
    Code:
    find "/home/user/directory" -type f -printf '%T@ %t %p\n' | sort -k 1 -nr | rev | cut -d' ' -f1 | rev
    What am I doing wrong? Is this even possible?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    Something like this?
    Code:
    #define STR(x)  #x
    #define COMMAND(dir) "find " STR(dir) " -type f -printf '%T@ %t %p\\n' | sort -k 1 -nr | rev | cut -d' ' -f1 | rev"
    #define DIRECTORY /home/user/directory
    
    int main ( ) {
      printf("%s\n", COMMAND(DIRECTORY));
    }
    
    
    
    $ gcc -E main.c
    # 1 "main.c"
    # 1 "<built-in>"
    # 1 "<command-line>"
    # 1 "/usr/include/stdc-predef.h" 1 3 4
    # 1 "<command-line>" 2
    # 1 "main.c"
    int main ( ) {
      printf("%s\n", "find " "/home/user/directory" " -type f -printf '%T@ %t %p\\n' | sort -k 1 -nr | rev | cut -d' ' -f1 | rev");
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    58
    Yes, that worked, but I found the following:
    Code:
    #define COMMAND(dir) "find " dir " -type f -printf '%T@ %t %p\\n' | sort -k 1 -nr | rev | cut -d' ' -f1 | rev"
    Includes the double quotes around the directory name (which I wasn't really concrened with, but is nice to have). It's without the pound symbol (#).

    Other than the double quotes being included, is there anything I need to be aware of if I choose to use "dir" without the preceeding pound symbol. Or is it safer to use STR(dir)?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,912
    I'd say that it makes more sense to #define DIRECTORY to be a string literal instead of a token that you then stringify: that way, if you want to say print DIRECTORY for some reason, you just do it. So yes, that combined with the direct approach of making use of the automatic concatenation of adjacent string literals is sensible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking a macro's sign in order to define other macro
    By Egalestrenge in forum C Programming
    Replies: 5
    Last Post: 05-23-2016, 09:39 PM
  2. Macro problem
    By TriKri in forum C Programming
    Replies: 6
    Last Post: 05-14-2010, 02:52 AM
  3. macro problem
    By eklavya8 in forum C Programming
    Replies: 11
    Last Post: 06-07-2009, 01:24 PM
  4. problem in macro
    By Bargi in forum C Programming
    Replies: 17
    Last Post: 02-04-2009, 10:17 AM
  5. macro problem
    By richdb in forum C Programming
    Replies: 11
    Last Post: 01-24-2006, 01:29 AM

Tags for this Thread