Re: [Dev-C++] List files and check if a directory exist.
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Matthias <mm...@ma...> - 2002-10-09 20:50:00
|
>>I haven't learned about it yet, but the solution is in >dirent.h, do a
> search ...
>
> Ok, i searched. But all examples that i found didn't compile.
Read up un 'stat', 'lstat' for information about files and directories
make sure that you link with libc.
Then read the paragraph about 'scandir' if you use Unix or
'FindFirstFile'/'FindNextFile' on Windows
----
int fl_filename_isdir(const char* n) {
struct stat s;
#ifdef WIN32
char fn[1024];
int length;
// This workaround brought to you by the fine folks at Microsoft!
// (read lots of sarcasm in that...)
length = strlen(n);
if (length < (int)(sizeof(fn) - 1)) {
if (length < 4 && isalpha(n[0]) && n[1] == ':' &&
(isdirsep(n[2]) || !n[2])) {
// Always use D:/ for drive letters
fn[0] = n[0];
strcpy(fn + 1, ":/");
n = fn;
} else if (length > 0 && isdirsep(n[length - 1])) {
// Strip trailing slash from name...
length --;
memcpy(fn, n, length);
fn[length] = '\0';
n = fn;
}
}
#endif
return !stat(n, &s) && (s.st_mode&0170000)==0040000;
}
|