View Full Version : [c++] file o cartella??
c'è una funzione per scoprire se una stringa è l'indirizzo di un file o di una cartella??
io ho provato con dati_file.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ma mi salta alcune cartelle... :confused:
ps: in php mi basterebbe un semplice is_file(string);
ho pensato di risolvere così... cosa ne pensate?? basta un ( ("va bene") o (un "non va bene" e il perchè) ) :p
int isfile(char *string) {
FILE *st = fopen(string,"rb");
if (st != NULL) return 1;
return 0;
}
ilsensine
18-10-2006, 16:25
dati_file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
Hai provato con GetFileAttributes()?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileattributes.asp
EDIT: questo se vuoi fare
if (GetFileAttributes(filename) & FILE_ATTRIBUTE_DIRECTORY)
altrimenti fai come ha detto ilsensine.
EDIT 2 corretto == con &
dati_file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
xche quel simbolo al posto di == ??? :mc:
xche quel simbolo al posto di == ??? :mc:
AND bit a bit...
ilsensine
18-10-2006, 16:34
xche quel simbolo al posto di == ??? :mc:
Perché dwFileAttributes contiene, nei suoi diversi bit, indicazioni su più attributi.
Ad es. per una directory in sola lettura e nascosta avrai che
dwFileAttributes == (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN)
Per testare i field singolarmente, devi quindi fare un "and" tra dwFileAttributes e il field che ti interessa.
perfetto... capito :cool:
provato e mi funziona...
grazie!
#include <sys/types.h>
#include <sys/stat.h>
int is_file (char *pathname)
{
struct stat st;
if (stat (pathname, &st) != 0)
return 0;
return st.st_mode & _S_IFREG ? 1 : 0;
}
int is_dir (char *pathname)
{
struct stat st;
if (stat (pathname, &st) != 0)
return 0;
return st.st_mode & _S_IFDIR ? 1 : 0;
}
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.