Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Dehaviour of DirectoryExists
Re: Dehaviour of DirectoryExists [message #44997 is a reply to message #44995] Wed, 12 August 2015 20:12 Go to previous messageGo to previous message
kov_serg is currently offline  kov_serg
Messages: 42
Registered: August 2008
Location: Russia
Member
I expected something like this

Windows:
#include <windows.h>
#include <stdio.h>

int is_dir(const char *szPath) {
  DWORD dwAttrib = GetFileAttributesA(szPath);
  return (dwAttrib != INVALID_FILE_ATTRIBUTES && 
         (dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
void test(const char* name) {
    printf("is_dir=%d : %s\n",is_dir(name),name);
}
int main(int argc,char** argv) {
	test("C:\\Windows");
	test("C:\\Windows\\");
	test("C:\\Windows\\.");
	test("C:\\Windows\\.\\");
	test("C:\\Windows\\none");
	test("\\\\?\\C:\\Windows");
	test("\\\\?\\C:\\Windows\\none");
	return 0;
}

Output:
is_dir=1 : C:\Windows
is_dir=1 : C:\Windows\
is_dir=1 : C:\Windows\.
is_dir=1 : C:\Windows\.\
is_dir=0 : C:\Windows\none
is_dir=1 : \\?\C:\Windows
is_dir=0 : \\?\C:\Windows\none


Linux:
include <sys/stat.h>
#include <stdio.h>

int is_dir(const char* name) {
    struct stat s;
    int err = stat(name, &s);
    if(-1 == err) return 0;
    return S_ISDIR(s.st_mode) ? 1:0;
}
void test(const char* name) {
    printf("is_dir=%d : %s\n",is_dir(name),name);
}
int main(int argc,char** argv) {
    test("/home");
    test("/home/");
    test("/home/.");
    test("/home/./");
    test("/home/none");
    return 0;
}

Output:
is_dir=1 : /home
is_dir=1 : /home/
is_dir=1 : /home/.
is_dir=1 : /home/./
is_dir=0 : /home/none


https://en.wikipedia.org/wiki/Principle_of_least_astonishmen t

[Updated on: Wed, 12 August 2015 20:14]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Previous Topic: Why Cout() and Cerr() work so different?
Next Topic: Suggestion for StringBuffer
Goto Forum:
  


Current Time: Sun Jun 22 12:12:49 CEST 2025

Total time taken to generate the page: 0.04422 seconds