Hi friends,
How can I get the time stamp of a file from C++ program.

Amit

By telling us which OS/Compiler you're working with for starters.
And you also need to make sure you have a file system which records such information.

By telling us which OS/Compiler you're working with for starters.
And you also need to make sure you have a file system which records such information.

Actually I am working in a project which will be OS independent.
So pls give me some solution which will work on windows as well as Solaris.

You're out of luck then, the answer is inherently platform dependent.

If it was platform independent, I wouldn't have needed to ask would I ?

use the stat() or fstat() standard C library functions.

Thanx for your answer.
Now suppose I've a pointer of ifstream. Is there are any way to get the timestamp of that file from that pointer?

You're out of luck then, the answer is inherently platform dependent.

If it was platform independent, I wouldn't have needed to ask would I ?

ok salem,
pls tell me the ways for Windows and Solaris platform.

Thanx for your answer.
Now suppose I've a pointer of ifstream. Is there are any way to get the timestamp of that file from that pointer?

The stat() function does not require an open file and is compatible for every standard compliant C and C++ compiler.

boost::filesystem library provides portable facilities to query and manipulate paths, files, and directories.
it supports operating systems which provide either the POSIX or Windows API and is in regular use on a number of platforms (including Microsoft Windows and Sun Solaris) using a variety of compilers. programs using the library are portable, both in the syntax and the semantics of program code.

tr2 has a proposal to add a filesystem library component to the C++ Standard Library. this proposal is based on boost::filesystem. http://www.boost.org/libs/filesystem/doc/tr2_proposal.html#Introduction

unlike most of boost, this is not a header only library. (much of the library code is platform specific. the right code is chosen based on the platform settings when building.)

here is how you could use the library to get the last modified time for a file.

#include <boost/filesystem/operations.hpp>
#include <ctime>
#include <iostream>
#include <fstream>

int main()
{
  boost::filesystem::path path( 
                    "/usr/local/include/boost/filesystem/operations.hpp" ) ;
  std::time_t t = boost::filesystem::last_write_time( path ) ;
  std::cout << "UTC: " << std::asctime( std::gmtime(&t) ) ;
  // note: some file system report last write time as local time, 
  // while others (eg. NTFS) report it as UTC. this can be checked 
  // by creating a new file and getting it's timestamp. eg.
  path = "/tmp/test_file" ;
  {
    std::ofstream file( path.file_string().c_str() );
    file << "test\n" ;
  }
  t = boost::filesystem::last_write_time( path ) ;
  std::cout << "just created UTC: " << std::asctime( std::gmtime(&t) ) ;
}
/**
>c++ -L/usr/local/lib -lboost_filesystem filesystem.cc && ./a.out
UTC: Tue Aug 14 13:17:48 2007
just created UTC: Mon Oct 15 15:13:39 2007
*/
This article has been dead for over six months. Start a new discussion instead.