INFO: Full Paths Need Double Backslashes in C

Last reviewed: August 26, 1997
Article ID: Q34305

The information in this article applies to:
  • Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, 6.0ax
  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 1.5
  • Microsoft Visual C++ 32-bit Edition, versions 1.0, 2.0, 4.0, 5.0

SUMMARY

In C, the backslash character (\) has a special meaning to treat the next character as a literal value or to create an escape sequence, such as the end-of-line indicator (\n). Therefore, you must use two sequential backslashes to separate the directory name from the file name in a path specification. For example, consider an attempt to open a file with the following code:

   stream = fopen( "c:\c\source\test.c", "r" );

This code fails because the fopen() function receives "C:CSOURCETEST.C" as its first argument. The function attempts to open a file in the current directory rather than opening the TEST.C file in the C:\C\SOURCE directory. The following code example creates the desired behavior:

   stream = fopen( "c:\\c\\source\\test.c", "r" );

MORE INFORMATION

The following C run-time library routines can accept a fully qualified path as an argument:

   *_dos_creat         chmod     execvpe   sopen      splitpath
   *_dos_creatnew      creat     fopen     spawnl     stat
   *_dos_findfirst     execl     freopen   spawnle    system
   *_dos_getfileattr   execle    mkdir     spawnlp    tempnam
   *_dos_open          execlp    open      spawnlpe   utime
   *_dos_setfileattr   execlpe   putenv    spawnv
    _searchenv         execv     remove    spawnve
    access             execve    rename    spawnvp
    chdir              execvp    rmdir     spawnvpe

* indicates the function is available only with 16-bit implemetations.

Keywords          : CLngIss
Version           : MS-DOS:5.1,6.0,6.00a,6.00ax,7.0; WINDOWS:1.0,1.5; WINDOWS  NT:1.0,2.0,4.0,5.0
Platform          : MS-DOS NT WINDOWS
Issue type        : kbinfo


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 26, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.