Hmm...im trying to check if a directory exist.
I used GetFileAttributes, failed...
Mind helping me? Thank you.
Hmm...im trying to check if a directory exist.
I used GetFileAttributes, failed...
Mind helping me? Thank you.
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.)
I mean, it didn't work actually.
Well, are you paying minimum wage? (Apologies to ... whoever had that in their sig, once upon a time.)
What does "didn't work" mean?
GetFileAttributes works. double check your escapes and \\ are all what they should be.
Doesn't work. , im trying my best.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 = ""; }
If the directory does not exist, the return value is not NULL. At least, I think that's the question you're asking.
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.
[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.
Code:BOOL isDirectory(char *pPath) { DWORD dwAttrib = GetFileAttributes(pPath); return dwAttrib != INVALID_FILE_ATTRIBUTES && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY); }
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
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; }