Thread: Passing to FindFirstFile not working

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    5

    Passing to FindFirstFile not working

    Hi guys...

    The following code works fine and ouputs a list of all the files within the c drive when i enter the directory myself i.e

    Code:
    void OpenDirectory(string File)
    {
       WIN32_FIND_DATA w32;
       ZeroMemory( &w32, sizeof(w32) );
       HANDLE h = FindFirstFile( "C:\\*.*", &w32 ); // Works with this
       string hold;
       hold = w32.cFileName;                       
       CheckFile(hold);                            
       while ( FindNextFile( h, &w32 ))            
       {
         hold = w32.cFileName;
         cout << hold << endl;   
       } 
    }
    However if i pass the drive in a string and then place that string into a char array, the program compiles but outputs nothing.

    Code:
    int main()
    {
          OpenDirectory("C:\\*.*");
          return 0;
    }
    
    void OpenDirectory(string File)
    {
       int length = File.size();
       char newFile[length];
       int i;
       for (i = 0; i < length; i++)
       {
          newFile[i] = File.at(i);
       }
       WIN32_FIND_DATA w32;
       ZeroMemory( &w32, sizeof(w32) );
       HANDLE h = FindFirstFile( newFile, &w32 ); // Doesn't work
       string hold;
       hold = w32.cFileName;                       
       CheckFile(hold);                            
       while ( FindNextFile( h, &w32 ))            
       {
         hold = w32.cFileName;
         cout << hold << endl;   
       } 
    }
    I've tried everything i could think of but i can't get it to output anything. If i try using the string without placing it into a char array the program doesn't even compile, anyone got any ideas

    Cheers Mike...

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    5
    Cheers Salem your a star its working now

    Mike...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. Max/min function not working correctly
    By En-Motion in forum C++ Programming
    Replies: 6
    Last Post: 03-19-2009, 12:28 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 01:25 AM
  5. Passing Parameters
    By fry in forum C++ Programming
    Replies: 2
    Last Post: 10-04-2002, 03:06 AM