Q34305: Path Strings Need Double Backslashes or fopen/open Fails
Article: Q34305
Product(s): See article
Version(s): 3.00 4.00 5.00 5.10 | 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 27-OCT-1988
Problem:
According to Pages 444 and 274 of the "Microsoft C 5.10 Optimizing
Compiler Run-Time Library Reference," I should be able to specify the
path to the file that I want to open. However, I always receive a null
pointer, indicating an error, when I attempt to open a file that is
not in the current directory.
I am using the following fopen statement:
stream = fopen("d:\c\source\test.c", "r");
Response:
The following fopen statement will successfully open the file:
stream = fopen("d:\\c\\source\\test.c", "r");
Because the backslash has special meaning in C, it must be preceded by
an additional backslash. In the case of the first (unsuccessful)
example, a file named "d:csourcetest.c" actually is being searched for
in the current directory. In the second example, the correct directory
is searched for in the file "test.c".
The following other functions are among those that use path names as
input arguments:
chdir, mkdir, rmdir, access, chmod, remove, rename, splitpath, stat,
creat, open, sopen, freopen, tempnam, execl, execle, execlp,
execlpe, execv, execve, execvp, execvpe, spawnl, spawnle, spawnlp,
spawnlpe, spawnv, spawnve, spawnvp, spawnvpe, system, _dos_creat,
_dos_creatnew, _dos_findfirst, _dos_getfileattr, _dos_open,
_dos_setfileattr, utime, putenv, _searchenv
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.
Copyright Microsoft Corporation 1986-2002.