Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How to check the last modified time of a file

Tags:

I'm caching some information from a file and I want to be able to check periodically if the file's content has been modified so that I can read the file again to get the new content if needed.

That's why I'm wondering if there is a way to get a file's last modified time in C++.

like image 767
Mr. Nicky Avatar asked Nov 09 '16 10:11

Mr. Nicky


People also ask

How can I tell what time a file was modified?

You can also see the modified date by viewing the file properties. Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.

Which function is used to get the last modification time?

Using getlastmod() Function: The getlastmod() function is used to get the last modification time of the current page.

Which command gives information about time of last modification done on file?

ls command ls – Listing contents of directory, this utility can list the files and directories and can even list all the status information about them including: date and time of modification or access, permissions, size, owner, group etc.

How do you get the last modified date of a file in Linux?

The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.

How to get last modified date and time for a file?

For example, to get the last modified time for the file ‘E:commands.docx’ the command would be: To get the modified date and time for all files and sub folders in the current directory the command would be: Using Forfiles command. Using forfiles command we can get modified date and time for all the files in a directory.

How to get the last write time of a file in C #?

To get the last write time of a file in C#, use the LastWriteTime () method. For this, use the FileInfo as well as DateTime classes. Create an object of each −

How to find the latest modified file in a directory?

You can run the below command to find the latest modified file in a directory. It would print the list of files in the order of file modified time. It would print the recently modified file at the bottom. /O:D will make the command print the files list using the file date/time attributes. /T:W will make the command use file modified time.

How do I get the date and time of a file?

To get the modified date and time for all files and sub folders in the current directory the command would be: dir /T:W. To get modified date/time only for files in the current directory(i.e exclude directories from files) dir /T:W /A:-D.


1 Answers

There is no language-specific way to do this, however the OS provides the required functionality. In a unix system, the stat function is what you need. There is an equivalent _stat function provided for windows under Visual Studio.

So here is code that would work for both:

#include <sys/types.h> #include <sys/stat.h> #ifndef WIN32 #include <unistd.h> #endif  #ifdef WIN32 #define stat _stat #endif  auto filename = "/path/to/file"; struct stat result; if(stat(filename.c_str(), &result)==0) {     auto mod_time = result.st_mtime;     ... } 
like image 56
Smeeheey Avatar answered Oct 16 '22 16:10

Smeeheey