Hi..
I am stuck with a problem where I need to extract some information from
a File in C++.e.g File size,date of creation, Location on the Disk etc.
I haven't been able to find any particular solution yet. Could anybody
amongst you help me in this regard. I'll be glad.
Regards;
Saleem
Try recls (
http://synesis.com.au/software/recls/). Its a recursive sile
sytem search library, cross-platform. You can set up a search and read
off the matches one at a time, using Iterator-like object, or using STL
container-like object.
To getting the details of a single file you write something like the
following:
#include <recls/cpp/filesearch.hpp>
int main(int argc, char **argv)
{
if(argc < 2)
{
return EXIT_FAILURE;
}
using namespace recls::cpp;
using namespace std;
FileEntry entry = FileSearch::Stat(argv[1]);
It gives you full information on path and components, including
absolute, and relative to the search directory.
cout << "full path: " << entry.GetPath() << endl;
cout << "search-relative path: " << entry.GetSearchRelativePath()
<< endl;
cout << "location: " << entry.GetDirectoryPath() << endl;
#ifdef RECLS_PLATFORM_API_WIN32
cout << "drive: " << entry.GetDrive() << endl;
#endif
cout << "directory: " << entry.GetDirectory() << endl;
cout << "file name+extension: " << entry.GetFile() << endl;
cout << "file name: " << entry.GetFileName() << endl;
/// and so on
One problem is that the size type is platform specific. So:
if(!entry.IsDirectory())
{
#ifdef RECLS_PLATFORM_API_WIN32
// recls_filesize_t is ULARGE_INTEGER
cout << "size: " << entry.GetSize().LowPart << endl;
#else
// recls_filesize_t is off_t
cout << "size: " << entry.GetSize() << endl;
#endif
The same problem (but) worse!) is that the time type is platform
specific. So:
#ifdef RECLS_PLATFORM_API_WIN32
// recls_time_t is FILETIME
// you have to convert using FileTimeToSystemTime
FILETIME t1 = entry.GetCreationTime();
FILETIME t2 = entry.GetModificationTime();
FILETIME t3 = entry.GetLastAccessTime();
FILETIME t4 = entry.GetLastStatusChangeTime();
#else
// recls_time_t is time_t
// you have to convert using gmtime
time_t t1 = entry.GetCreationTime();
time_t t2 = entry.GetModificationTime();
time_t t3 = entry.GetLastAccessTime();
time_t t4 = entry.GetLastStatusChangeTime();
#endif
If you have a cross-platform time class you can just convert to your
time class and not worry about it. Date/time is such a drag. recls
seems to just ignore the problema dn leave it to the user.
If you can get round the time/size hasslles, it's otherwise good. I use
it for recusrive searches whenever I need it.