Checking if an entry is a file or a directory
This applies to linux C++ programming...
If I had a string containing: /var/project/something
How could I determine if the entry the string describes (something) is the name of a directory or a file?
I've tried opening known directories using ifstream, however even if I open a directory, all the good bits are set and it does not fail... any ideas?
When I was writing a shell for uni, one of the things we had to do before executing a program was to see if it file existed in the specified path. We used the function 'stat'.
I'm on Win right now, but here's part of the info from the cd version of MSDN, on one of the stat structure members:
So, it looks like this should do well for what you need.
I'm on Win right now, but here's part of the info from the cd version of MSDN, on one of the stat structure members:
Quote:
st_mode
Bit mask for file-mode information. The _S_IFDIR bit is set if path specifies a directory; the _S_IFREG bit is set if path specifies an ordinary file or a device. User read/write bits are set according to the file’s permission mode; user execute bits are set according to the filename extension.
So, it looks like this should do well for what you need.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement