Help required on Extracting File Information

S

saleemahmad70

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
 
M

Mike Wahler

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
 
R

red floyd

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-lite/how-to-post.html#faq-5.9
 
V

Victor Bazarov

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
 
R

r

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.
 
J

JimB

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.
 
S

saleemahmad70

JimB said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top