User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,982 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,740 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2459 | Replies: 8
Reply
Join Date: May 2005
Location: Kolkata
Posts: 33
Reputation: amt_muk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Help LastModified Timestamp of a file

  #1  
Oct 15th, 2007
Hi friends,
How can I get the time stamp of a file from C++ program.

Amit
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,346
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 379
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: LastModified Timestamp of a file

  #2  
Oct 15th, 2007
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,644
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 869
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: LastModified Timestamp of a file

  #3  
Oct 15th, 2007
use the stat() or fstat() standard C library functions.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: May 2005
Location: Kolkata
Posts: 33
Reputation: amt_muk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: LastModified Timestamp of a file

  #4  
Oct 15th, 2007
Originally Posted by Salem View Post
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.
Reply With Quote  
Join Date: Dec 2005
Posts: 3,346
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 379
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: LastModified Timestamp of a file

  #5  
Oct 15th, 2007
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 ?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: May 2005
Location: Kolkata
Posts: 33
Reputation: amt_muk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: LastModified Timestamp of a file

  #6  
Oct 15th, 2007
Originally Posted by Ancient Dragon View Post
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?
Reply With Quote  
Join Date: May 2005
Location: Kolkata
Posts: 33
Reputation: amt_muk is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
amt_muk amt_muk is offline Offline
Light Poster

Re: LastModified Timestamp of a file

  #7  
Oct 15th, 2007
Originally Posted by Salem View Post
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 10,644
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 36
Solved Threads: 869
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: LastModified Timestamp of a file

  #8  
Oct 15th, 2007
Originally Posted by amt_muk View Post
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.
Last edited by Ancient Dragon : Oct 15th, 2007 at 8:06 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Dec 2006
Location: india
Posts: 1,056
Reputation: vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light vijayan121 is a glorious beacon of light 
Rep Power: 9
Solved Threads: 159
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: LastModified Timestamp of a file

  #9  
Oct 15th, 2007
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...l#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.
  1. #include <boost/filesystem/operations.hpp>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. int main()
  7. {
  8. boost::filesystem::path path(
  9. "/usr/local/include/boost/filesystem/operations.hpp" ) ;
  10. std::time_t t = boost::filesystem::last_write_time( path ) ;
  11. std::cout << "UTC: " << std::asctime( std::gmtime(&t) ) ;
  12. // note: some file system report last write time as local time,
  13. // while others (eg. NTFS) report it as UTC. this can be checked
  14. // by creating a new file and getting it's timestamp. eg.
  15. path = "/tmp/test_file" ;
  16. {
  17. std::ofstream file( path.file_string().c_str() );
  18. file << "test\n" ;
  19. }
  20. t = boost::filesystem::last_write_time( path ) ;
  21. std::cout << "just created UTC: " << std::asctime( std::gmtime(&t) ) ;
  22. }
  23. /**
  24. >c++ -L/usr/local/lib -lboost_filesystem filesystem.cc && ./a.out
  25. UTC: Tue Aug 14 13:17:48 2007
  26. just created UTC: Mon Oct 15 15:13:39 2007
  27. */
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:02 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC