Microsoft KB Archive/168439

= Using the C Run-time function stat to check a directory fails when the name passed to the function ends with a backslash (\) =

Article ID: 168439

Article Last Modified on 9/7/2005

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 5.0 Professional Edition

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2002 Standard Edition</li></ul>

 Microsoft Visual C++ .NET 2003 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q168439

<div class="symptoms_section">

SYMPTOMS
Using the C Run-time function stat to check a directory fails when the name passed to the function ends with "\".

For example, _stat("\\my_directory\\my_name",&buf) returns OK, but _stat("\\my_directory\\my_name\\",&buf) will return -1 as error.

<div class="cause_section">

CAUSE
The _stat function calls the windows API function FindFirstFile passing it the path name. The FindFirstFile returns an invalid handle if the name ends with "\."

<div class="resolution_section">

RESOLUTION
Remove the trailing "\" from the path name.

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

MORE INFORMATION
The following sample program demonstrates the behavior. //Sample.cpp /* Compiler Options : none */ HANDLE           hSearch; WIN32_FIND_DATA  Buf; int main( void ) {  struct _stat buf; int result;
 * 1) include <sys/types.h>
 * 2) include <sys/stat.h>
 * 3) include <stdio.h>
 * 4) include <windows.h>

result = _stat( "c:\\temp", &buf ); if( result != 0 ) printf( "_stat function on c:\\temp failed " );

result = _stat( "c:\\temp\\", &buf ); if( result != 0 ) printf( "_stat function on c:\\temp\\ failed " );

hSearch= FindFirstFile((LPSTR)"c:\\temp", &Buf); if (hSearch == INVALID_HANDLE_VALUE ) printf("\n\n FindFirstFile on c:\\temp failed too");

hSearch= FindFirstFile((LPSTR)"c:\\temp\\", &Buf); if (hSearch == INVALID_HANDLE_VALUE ) printf("\n\n FindFirstFile on c:\\temp\\ failed too");

return 0; } Program output:

If you have a valid C:\temp directory: _stat function on c:\temp\ failed.

FindFirstFile on c:\temp\ failed, too. If you experience the C1010 error when you compile the sample code in Visual Studio .NET, click the following article number to view the article in the Microsoft Knowledge Base:

815644 Most common causes of C1010 error

Keywords: kbtshoot kbprb KB168439

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.