Check if a directory exist?

This is a discussion on Check if a directory exist? within the C++ Programming forums, part of the General Programming Boards category; Hmm...im trying to check if a directory exist. I used GetFileAttributes, failed... Mind helping me? Thank you....

  1. #1
    deletedforumuser
    Guest

    Check if a directory exist?

    Hmm...im trying to check if a directory exist.

    I used GetFileAttributes, failed...

    Mind helping me? Thank you.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If the function fails, the directory doesn't exist, right? So ... what?

    (And by the way, eventually you will learn: Windows questions go on the Windows forum.)

  3. #3
    deletedforumuser
    Guest
    I mean, it didn't work actually.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, are you paying minimum wage? (Apologies to ... whoever had that in their sig, once upon a time.)

    What does "didn't work" mean?

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    GetFileAttributes works. double check your escapes and \\ are all what they should be.

  6. #6
    deletedforumuser
    Guest
    Code:
    			sprintf_s(directory, "Accounts/%s",Client[i].Username.c_str());
    
    			sprintf_s(checkdirectory, "Accounts\\%s", Client[i].Username.c_str());
    
    			if(GetFileAttributes(checkdirectory) != NULL)
    			{
    
    				Send(i, "UsernameExist");
    			}
    			else
    			{
    
    
    				CreateDirectory(directory, NULL);
    
    				sprintf_s(directory, "Accounts/%s/Password.txt", Client[i].Username.c_str());
    
    				ofstream file(directory);
    
    				file << Client[i].Password;
    
    				file.close();
    
    				Client[i].Username = "";
    				Client[i].Password = "";
    
    			}
    Doesn't work. , im trying my best.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Shouldn't you use only \\? Why do you also use /?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If the directory does not exist, the return value is not NULL. At least, I think that's the question you're asking.

  9. #9
    deletedforumuser
    Guest
    Nevermind, that wasn't the problem...i found the problem by first printing it...then, i checked the value...and saw that 4294967295 was the value to Does not exist.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [quote="MSDN"]
    If the function succeeds, the return value contains the attributes of the specified file or directory.

    If the function fails, the return value is INVALID_FILE_ATTRIBUTES. To get extended error information, call GetLastError.
    [/code]
    The function doesn't return a pointer, so comparing with NULL, whilst it may not generate an error (in C++, since NULL is generally defined as 0), it is not the same as INVALID_FILE_ATTRIBUTES, which appears to be very similar to a 32-bit value of -1 when it's displayed as an unsigned.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    Code:
    BOOL isDirectory(char *pPath)
    {
        DWORD dwAttrib =  GetFileAttributes(pPath);
        return  dwAttrib !=  INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
    }

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    Shouldn't you use only \\? Why do you also use /?
    Because thankfully the Windows API supports '/' as directory separators, so that makes it a lot easier to port code between UNIX and Windows, and it saves some typing.
    I ONLY use '/' in my code.

    As for GetFileAttributes(), you could make your code more portable if you switched to the stat() (a.k.a. _stat() on Windows) function instead.
    Last edited by cpjust; 10-25-2008 at 02:48 PM.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    3
    I just tried the following code a few days ago. It dont know if its the best example, but it worked for me:

    Code:
      ofstream newFile (path, ios::trunc);	
    				
      // path found?
      if(newFile.good() == false) 
      {
        cout << "Directory doesn't exist. Closing wizard" << endl;
        break;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Check directory program is run from?
    By willc0de4food in forum C Programming
    Replies: 10
    Last Post: 06-29-2005, 02:36 AM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 05:44 PM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

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