I want to write a program to get the details of files and folder I created including the creation date like this: Feb 15 2006 What should I do? Any suggestion? And I have to mentioned that I'm not allowed to use windows.h .
-
1What do you mean not allowed? – starsplusplus Jan 16 '14 at 10:24
-
@starsplusplus He probably means that the code must be portable. In which case, there is no solution. – James Kanze Jan 16 '14 at 10:36
-
@JamesKanze More likely, this is a computer lab excercise and either this is the task specification, or indeed the lab is in other OS than Windows. – the swine Jan 16 '14 at 10:45
-
@theswine It's likely a computer lab exercise, but if I were running a computer lab, I'd insist on portable code (and thus no `
`), regardless of what the platforms were. – James Kanze Jan 16 '14 at 16:34 -
@JamesKanze Whoa. Didn't you just say below that it "isn't possible, because not all systems even maintain this information."? – the swine Jan 16 '14 at 17:13
-
1@theswine Exact. Which means that I wouldn't require it as homework in a lab. (Or... in an advanced course, I might require something like this to teach people how to handle system dependencies. But that would mean including `
` in the Windows code, and providing some sort of mechanism for not requiring the information where it isn't available. Something for a fairly advanced course only.) – James Kanze Jan 16 '14 at 17:51 -
I found this workaround: https://stackoverflow.com/a/40504396/1422630 – Aquarius Power Apr 20 '18 at 18:32
3 Answers
In Linux-like system, you can use the stat function, as such:
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
int main(int argc, char **argv)
{
struct stat t_stat;
stat("file_name", &t_stat);
struct tm * timeinfo = localtime(&t_stat.st_ctime); // or gmtime() depending on what you want
printf("File time and date: %s", asctime(timeinfo));
return 0;
}
In Windows, I'd suggest using the system() function and get file time from commandline:
#include <stdlib.h>
int main(int argc, char **argv)
{
system("dir /T:C file_name");
return 0;
}
You can redirect output of system() to e.g. a temporary file and parse the date from there.
Or use this workaround https://stackoverflow.com/a/40504396/1422630, that makes windows' _stat
compatible with linux one stat
, basically:
#ifdef WIN32
#define stat _stat
#endif

- 3,729
- 5
- 32
- 67

- 10,713
- 7
- 58
- 100
-
1In Windows, there's no reason not to use `GetFileAttributesEx`. The fact that he cannot use `
` probably means that he needs a solution which will work on non-Windows platforms. Which isn't possible, because not all systems even maintain this information. – James Kanze Jan 16 '14 at 10:39 -
@JamesKanze Well I would use that, but since he asked for not using windows.h, this is just another way to do it. He did not say which platform he wants so i provided solutions that would work on linux / mac / windows and he can pick one. Anyway, one would use some multiplatform library, where such functionality would be encapsulated with uniform interface and of course the implementation would be appropriately ifdef-ed (or configured in build system) differently for each platform. – the swine Jan 16 '14 at 10:44
-
1spent 3h to understand `stat()` was failing with `file not found`, but this answer helped anyway, thx :) – Aquarius Power Apr 19 '18 at 02:12
-
-
In general it's bad practice to rely on executing commands from within a program and parsing the results IMO. – Jun 26 '20 at 22:37
-
@user13709754 Yeah, agree. But the assignment here was quite contrived. One would just use native API for this. On the other hand, writing a shell script would do exactly that (run a command and parse)--and we have a lot of shell scripts. And the world is still spinning :). – the swine Jul 07 '20 at 20:41
The simple answer is that you can't. Depending on the system,
there may be a system specific function which will do this
(e.g. GetFileAttributesEx
under Windows), but not all systems
even support it; Unix, for example, does not keep this
information, and there is no way to obtain it on a Unix system
(or on a file system which is hosted on a Unix system—I
don't know what the Windows function will return if the file is
physically hosted on a Unix system).

- 150,581
- 18
- 184
- 329
-
Most Unix file systems don't store the creation time, although some, such as HFS+, ZFS, and UFS2 do. (wiki) – the swine Jan 16 '14 at 10:48
-
@theswine Interesting. Posix doesn't support it. Beyond that, my recent (in the last fifteen years) experience with Unix has been limited to Solaris and Linux, so I'm not aware of extensions elsewhere. – James Kanze Jan 16 '14 at 11:01
-
@JamesKenze I was quite sure it was working in Ubuntu, FreeBSD, Red Hat and Cent OS. Well, at least sure that the `st_ctime` field is there, not so sure what would it contain. One usually needs the `st_mtime`, that should work pretty much anywhere. – the swine Jan 16 '14 at 11:07
-
@theswine I'll admit that my "man pages" are the Posix standard, even when I'm working under Linux. (I've done very little work under Linux; I've mainly worked with Solaris.) – James Kanze Jan 16 '14 at 11:14
-
Hey guys. With"I'm not allowed to use windows.h" I mean this is my homework and my teacher told not to use windows.h and system.h. So what do you suggest me? And I use windows to compile it not Linux or such operating system. – user3202024 Jan 16 '14 at 11:51
-
@user3202024 The answer is: there is no portable solution. Any solution on Windows will require Windows specific code, which means including `
`. (You could, of course, copy/paste the declaration of the function from ` – James Kanze Jan 16 '14 at 16:32` into your own code, but that would be cheating.) -
@James Kanze:you mean I can't do it without Windows.h? I don't want to cheat by just copy and paste, of course I want to learn it. I need any idea to do it by myself! – user3202024 Jan 16 '14 at 18:05
-
@user3202024 You need to have a declaration for `GetFileAttributesEx`, in order to call it. You _can_ write that declaration by hand, or copy it out of `
`, but both of these solutions are very, very bad programming practices; if you need to call `GetFileAttributesEx`, you need to include ` – James Kanze Jan 16 '14 at 18:19`. (Presumably, if the file is on a local disk, and you have administrator rights, it's also possible to read the information off the local disk. This is _not_ something for a beginner, however, since it entails rewriting large parts of the OS.) -
Thanks all you answered me. You gave me good ideas. I'll try to write the program. – user3202024 Jan 17 '14 at 11:19
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
struct tm* clock; // create a time structure
struct stat attrib; // create a file attribute structure
stat("afile.txt", &attrib); // get the attributes of afile.txt
clock = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure
// clock->tm_year returns the year (since 1900)
// clock->tm_mon returns the month (January = 0)
// clock->tm_mday returns the day of the month

- 739
- 1
- 6
- 19
-
This is a purely Unix solution, not portable. And it returns the last modified time, not the creation time (which isn't available on Unix systems). – James Kanze Jan 16 '14 at 10:35
-