Msdn example code (FindFirstFile, FindNextFile

This is a discussion on Msdn example code (FindFirstFile, FindNextFile within the Windows Programming forums, part of the Platform Specific Boards category; Hi guys... im trying to understand some win32api functions and i saw this code in msdn page: Code: #include <windows.h> ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    17

    Msdn example code (FindFirstFile, FindNextFile

    Hi guys... im trying to understand some win32api functions and i saw this code in msdn page:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <strsafe.h>
    #include <malloc.h>
    
    #define BUFSIZE MAX_PATH
    
    int main(int argc, char *argv[])
    {
       WIN32_FIND_DATA FindFileData;
       HANDLE hFind = INVALID_HANDLE_VALUE;
       DWORD dwError;
       LPSTR DirSpec;
       size_t length_of_arg;
    
    
       DirSpec = (LPSTR) malloc (BUFSIZE);
       
       // Check for command-line parameter; otherwise, print usage.
       if(argc != 2)
       {
          printf("Usage: Test <dir>\n");
          return 2;
       }
    
       // Check that the input is not larger than allowed.
       StringCbLength(argv[1], BUFSIZE, &length_of_arg);
       if (length_of_arg > (BUFSIZE - 2))
       {
          printf("Input directory is too large.\n");
          return 3;
       }
    
       printf ("Target directory is %s.\n", argv[1]);
    
       // Prepare string for use with FindFile functions.  First, 
       // copy the string to a buffer, then append '\*' to the 
       // directory name.
       StringCbCopyN (DirSpec, BUFSIZE, argv[1], length_of_arg+1);
       StringCbCatN (DirSpec, BUFSIZE, "\\*", 3);
    
       // Find the first file in the directory.
       hFind = FindFirstFile(DirSpec, &FindFileData);
    
       if (hFind == INVALID_HANDLE_VALUE) 
       {
          printf ("Invalid file handle. Error is %u.\n", GetLastError());
          return (-1);
       } 
       else 
       {
          printf ("First file name is %s.\n", FindFileData.cFileName);
          
    						// List all the other files in the directory.
          while (FindNextFile(hFind, &FindFileData) != 0) 
          {
             printf ("Next file name is %s.\n", FindFileData.cFileName);
          }
        
          dwError = GetLastError();
          FindClose(hFind);
          if (dwError != ERROR_NO_MORE_FILES) 
          {
             printf ("FindNextFile error. Error is %u.\n", dwError);
             return (-1);
          }
       }
    
       free(DirSpec);
       return (0);
    }
    Well, this code is for return all files in a directory... but look at the output:

    Code:
    Target directory is c:\test.
    Fist file name is ..
    Next file name is ...
    Next file name is testdirectory1.
    Next file name is tesfile1.bmp
    Next file name is testfile2.bmp
    Why the 2nd and 3rd lines of the input goes like this?
    How to fix it?
    Thanks =]

  2. #2
    Registered Luser risby's Avatar
    Join Date
    Jun 2006
    Posts
    72
    Quote Originally Posted by Probose
    printf ("First file name is %s.\n", FindFileData.cFileName);
    printf ("Next file name is %s.\n", FindFileData.cFileName);
    [/code]
    Well, this code is for return all files in a directory... but look at the output:

    Code:
    Target directory is c:\test.
    Fist file name is ..
    Next file name is ...
    Next file name is testdirectory1.
    Next file name is tesfile1.bmp
    Next file name is testfile2.bmp
    Why the 2nd and 3rd lines of the input goes like this?
    How to fix it?
    Thanks =]
    It's not a problem. The functions return all files and directories in a given directory. All directories contain a reference to themselves which is "." and a reference to their parent directory which is "..". The output contains ".." and "..." simply because the printf format specifier ends with a fullstop.

    You will see these special directories with the command "dir" in a dos command window or with "ls -a" in a terminal window on Unix.
    ===
    Don't grumble about what you can't have;
    be grateful you don't get what you deserve.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    17
    K, i got it risby =] Thanks
    And sorry for posting in the wrong section.
    Cya

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 08:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 05:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21