Microsoft KB Archive/34305

From BetaArchive Wiki

INFO: Full Paths Need Double Backslashes in C

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          : kbLangC kbVC 
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

Last Reviewed: August 28, 1997
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.