Macro to indicate directories in filenames

Discussion in 'C Programming' started by raphfrk, Mar 28, 2008.

  1. raphfrk

    raphfrk Guest

    I was wondering if there is a standard macro that will evaluate to the
    directory separator character in the OS that the compiler is
    targeting.

    For example, in UNIX, it would be / while in windows it would be \

    A filename could then be built up using

    sprintf( filename , "directory%sfilename", MACRO );

    Alternatively, is there a clean way to do it ?
     
    raphfrk, Mar 28, 2008
    #1
    1. Advertising

  2. raphfrk

    Ian Collins Guest

    raphfrk wrote:
    > I was wondering if there is a standard macro that will evaluate to the
    > directory separator character in the OS that the compiler is
    > targeting.
    >
    > For example, in UNIX, it would be / while in windows it would be \
    >
    > A filename could then be built up using
    >
    > sprintf( filename , "directory%sfilename", MACRO );
    >
    > Alternatively, is there a clean way to do it ?


    Yes, use '/', windows compilers accept both forms.

    --
    Ian Collins.
     
    Ian Collins, Mar 28, 2008
    #2
    1. Advertising

  3. raphfrk said:

    > I was wondering if there is a standard macro that will evaluate to the
    > directory separator character in the OS that the compiler is
    > targeting.


    There isn't, for the very simple reason that the Standard doesn't
    acknowledge the existence of directories. (Some systems really, truly,
    honestly don't have them. They have other ways of organising files.)

    > For example, in UNIX, it would be / while in windows it would be \
    >
    > A filename could then be built up using
    >
    > sprintf( filename , "directory%sfilename", MACRO );
    >
    > Alternatively, is there a clean way to do it ?


    If your targets are Windows and Unix, just use the / separator - it works
    fine on both. (It doesn't actually work on the Windows command line for
    ordinary "DOS"-style commands, but it works just fine within a C program.)

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Mar 28, 2008
    #3
  4. raphfrk

    raphfrk Guest

    On Mar 28, 10:59 pm, Richard Heathfield <> wrote:
    > > Alternatively, is there a clean way to do it ?

    >
    > If your targets are Windows and Unix, just use the / separator


    cool, thanks
     
    raphfrk, Mar 28, 2008
    #4
  5. raphfrk wrote:
    > I was wondering if there is a standard macro that will evaluate to the
    > directory separator character in the OS that the compiler is
    > targeting.


    Not possible. Consider that VMS uses the form

    server::disk:[000000.dir1.dir2]filename.ext;version

    and other OSen use even more amusing variants.

    For windows and *nix, as others have pointed out, "/" is fine, windows
    only pretends not to like /.

    --
    Mark McIntyre

    CLC FAQ <http://c-faq.com/>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Mar 28, 2008
    #5
  6. raphfrk

    Eric Sosman Guest

    raphfrk wrote:
    > I was wondering if there is a standard macro that will evaluate to the
    > directory separator character in the OS that the compiler is
    > targeting.
    >
    > For example, in UNIX, it would be / while in windows it would be \
    >
    > A filename could then be built up using
    >
    > sprintf( filename , "directory%sfilename", MACRO );
    >
    > Alternatively, is there a clean way to do it ?


    Anything that can produce

    SYS$DISK:<rootdir.>[topdir.directory]filename.ext;-1

    suffices.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Mar 28, 2008
    #6
  7. raphfrk

    Joe Wright Guest

    Mark McIntyre wrote:
    > raphfrk wrote:
    >> I was wondering if there is a standard macro that will evaluate to the
    >> directory separator character in the OS that the compiler is
    >> targeting.

    >
    > Not possible. Consider that VMS uses the form
    >
    > server::disk:[000000.dir1.dir2]filename.ext;version
    >
    > and other OSen use even more amusing variants.
    >
    > For windows and *nix, as others have pointed out, "/" is fine, windows
    > only pretends not to like /.
    >

    It is the Windows command processors, command.com and cmd.exe which
    demand '\' separators. The OS as far back as MSDOS 2.0 I think, is quite
    happy with the '/' separator.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Mar 29, 2008
    #7
  8. Ian Collins <> writes:
    > raphfrk wrote:
    >> I was wondering if there is a standard macro that will evaluate to the
    >> directory separator character in the OS that the compiler is
    >> targeting.
    >>
    >> For example, in UNIX, it would be / while in windows it would be \
    >>
    >> A filename could then be built up using
    >>
    >> sprintf( filename , "directory%sfilename", MACRO );
    >>
    >> Alternatively, is there a clean way to do it ?

    >
    > Yes, use '/', windows compilers accept both forms.


    <OT>
    If you're using it to open a file, it's probably ok. If you intend to
    display it to a user, possibly a Windows user who's not aware of this
    particular hidden feature of Windows, you might want to consider
    going to some extra effort to use '\\'.
    </OT>

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Mar 29, 2008
    #8
  9. On 29 Mar 2008 at 0:29, Joe Wright wrote:
    > Mark McIntyre wrote:
    >> raphfrk wrote:
    >>> I was wondering if there is a standard macro that will evaluate to the
    >>> directory separator character in the OS that the compiler is
    >>> targeting.

    >>
    >> Not possible. Consider that VMS uses the form
    >>
    >> server::disk:[000000.dir1.dir2]filename.ext;version
    >>
    >> and other OSen use even more amusing variants.
    >>
    >> For windows and *nix, as others have pointed out, "/" is fine, windows
    >> only pretends not to like /.
    >>

    > It is the Windows command processors, command.com and cmd.exe which
    > demand '\' separators. The OS as far back as MSDOS 2.0 I think, is quite
    > happy with the '/' separator.


    That must be useful for CBF then - I think he's upgraded to MSDOS 2.0
    now, hasn't he?
     
    Antoninus Twink, Mar 29, 2008
    #9
    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. Vlajko Knezic
    Replies:
    1
    Views:
    10,635
    Joe Smith
    Mar 6, 2005
  2. Miguel Dias Moura
    Replies:
    0
    Views:
    366
    Miguel Dias Moura
    May 24, 2004
  3. B.J.
    Replies:
    4
    Views:
    745
    Toby Inkster
    Apr 23, 2005
  4. anthonyberet
    Replies:
    5
    Views:
    428
    anthonyberet
    Feb 8, 2005
  5. Deepu
    Replies:
    10
    Views:
    167
    Xicheng Jia
    Mar 16, 2007
Loading...

Share This Page