Macro to indicate directories in filenames

R

raphfrk

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 ?
 
I

Ian Collins

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.

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

Richard Heathfield

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

Mark McIntyre

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.

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

Eric Sosman

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.

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

Joe Wright

Mark said:
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.

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

Keith Thompson

Ian Collins said:
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>
 
A

Antoninus Twink

Mark said:
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.

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?
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top