## Search the FAQ Archives

3 - A - B - C - D - E - F - G - H - I - J - K - L - M
N - O - P - Q - R - S - T - U - V - W - X - Y - Z

# comp.os.msdos.programmer FAQ part 3/5Section - - Why won't my C program open a file with a path?

( Part1 - Part2 - Part3 - Part4 - Part5 - Single Page )
[ Usenet FAQs | Web FAQs | Documents | RFC Index | Business Photos and Profiles ]

Top Document: comp.os.msdos.programmer FAQ part 3/5
Previous Document: - How can a batch file test existence of a directory?
Next Document: - How can I redirect printer output to a file?

You've probably got something like the following code:

char *filename = "c:\foo\bar\mumble.dat";
FILE *fptr;
/*.*/
fptr = fopen(filename, "r");

The problem is that \f is a form feed, \b is a backspace, and \m is m.
Whenever you want a backslash in a string constant in C, you must use
two backslashes:

char *filename = "c:\\foo\\bar\\mumble.dat";

This is a feature of every C compiler, because Dennis Ritchie designed C
this way. It's a problem only on MS-DOS systems, because only DOS (and
Atari ST/TT running TOS) uses the backslash in directory paths. But even
in DOS this backslash convention applies _only_ to string constants in
your source code. For file and keyboard input at run time, \ is just a
normal character, so users running your program would type in file specs
the same way as in DOS commands, with single \ characters.

Another possibility is to code all paths in source programs with /
rather than \ characters:

char *filename = "c:/foo/bar/mumble.dat";

Ralf Brown writes, "All versions of the DOS kernel accept either forward
or backslashes as directory separators. I tend to use this form more
frequently than backslashes since it is easier to type and read." This
applies to DOS function calls (and therefore to calls to the file
library of every programming language), but not to DOS commands.



## User Contributions:

Top Document: comp.os.msdos.programmer FAQ part 3/5
Previous Document: - How can a batch file test existence of a directory?
Next Document: - How can I redirect printer output to a file?

Part1 - Part2 - Part3 - Part4 - Part5 - Single Page

[ Usenet FAQs | Web FAQs | Documents | RFC Index ]

Send corrections/additions to the FAQ Maintainer:
jeffrey@carlyle.org (Jeffrey Carlyle)

Last Update March 27 2014 @ 02:11 PM