By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,429 Members | 1,467 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,429 IT Pros & Developers. It's quick & easy.

Help required on Extracting File Information

P: n/a
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

Dec 21 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

<sa***********@gmail.comwrote in message
news:11**********************@48g2000cwx.googlegro ups.com...
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.
None of what you want to do is directly doable with
Standard C++, the topic here. You can come reasonably
close to 'file size', in that you can determine how
many characters (i.e. bytes) can be read from a file,
which might or might not exactly correlate to your
operating system's notion of 'file size'.

All the rest, you'll need to use features of your
operating system, some or all of which are likely
provided as extensions to your implementation's
library.

Check your documentation, and/or support resources
devoted to your implementation and/or operating system.

-Mike
Dec 21 '06 #2

P: n/a
sa***********@gmail.com wrote:
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.
This is not possible in Standard C++ without using platform specific
libraries.

May I suggest that you ask in a newsgroup dedicated to your platform.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.9
Dec 21 '06 #3

P: n/a
sa***********@gmail.com wrote:
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.
Somebody in the newsgroup dedicated to your OS should be able to
help you. Things like file date, etc. are OS-specific. You could
try opening the file, positioning your stream to the end of the
file and getting the offset, thus obtaining the size of the file,
but that's about as much as you can do in Standard C++.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 21 '06 #4

P: n/a
r
sa***********@gmail.com wrote:
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
There is a filesystem library in boost that does some of the things you
need.

Dec 22 '06 #5

P: n/a
sa***********@gmail.com wrote:
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.

Dec 23 '06 #6

P: n/a

JimB wrote:
sa***********@gmail.com wrote:
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.

Thanks a lot u guys for ur kind help. I m really glad that u came up
with nice ideas n solutions. Thanks again;
Regards;
Saleem

Dec 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.