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

Amit

Dani AI

Generated

If you want one code path that works on Windows and Solaris, the modern choice is C++17/20 std::filesystem. Use std::filesystem::last_write_time() to fetch the last-modified timestamp; it is defined in the standard and maps to the native API on each OS. In C++20, convert its file_time_type to system_clock with std::chrono::clock_cast (or the implementation’s file_clock::to_sys) and then format as local time. (en.cppreference.com)

Example (C++20):

#include <filesystem>
#include <chrono>
#include <iostream>
#include <iomanip>

int main() {
  namespace fs = std::filesystem;
  std::error_code ec;
  fs::path p = "path/to/file";
  auto ft = fs::last_write_time(p, ec);           // portable API
  if (ec) return 1;
  auto st = std::chrono::clock_cast<std::chrono::system_clock>(ft);
  std::time_t tt = std::chrono::system_clock::to_time_t(st);
  std::cout << "Last modified: "
            << std::put_time(std::localtime(&tt), "%F %T") << '\n';
}

Notes and gotchas:

  • is right that semantics differ by FS. On Windows, NTFS stores file times in UTC; FAT stores them in local time. That can affect what you see after DST changes unless you convert carefully. Microsoft recommends converting via FileTimeToSystemTime and SystemTimeToTzSpecificLocalTime for correct local-time display. (learn.microsoft.com)
  • ’s POSIX tip still applies when you cannot use C++17: stat/fstat give you st_mtim (nanosecond-resolution last modification time). Solaris follows this POSIX spec. (pubs.opengroup.org)
  • : there is no portable way to get a timestamp from an ifstream* alone. Streams do not expose native handles. Keep the path and call last_write_time() on it; if you truly must use an open handle, fall back to platform APIs (POSIX fstat, or on Windows GetFileTime). (pubs.opengroup.org)

If you are stuck on pre-C++17 toolchains, ’s suggestion of Boost.Filesystem remains a solid compatibility layer until you can move to the standard library.

Recommended Answers

All 8 Replies

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.

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

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.

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
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.