yangye
asked on
How to read file properties in C ?
I need to read the file properties in C. Specifically, I need to read the times of creation, modification of the file. Does anyone know which function can achive this in C language? I know the functions suitable for this in Visual Basic, but I don't know how to do it in C.
Thanks in advance for your help.
Thanks in advance for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks cameron_schuler and timbauer. I just need to do it on DOS (standard C), so I think stat() is a suitable one. Now I have a question, how can I print out the time_t value like a string? I.e., the last modified time of the file "st_mttime" is in time_t structure, I need to record that in a file, so I tried to use
char filename[100];
char recordtime[100];
struct stat buf[1000];
......
stat (filename, buf);
strcpy(recordtime, but->st_mttime);
then tried to write the recordtime to a file (I know how to do this).
......
When I debug, stat(filename, buf) was successeed, but strcpy(recordtime, buf->st_mttime) was failed. Any suggestions? Thanks a lot.
char filename[100];
char recordtime[100];
struct stat buf[1000];
......
stat (filename, buf);
strcpy(recordtime, but->st_mttime);
then tried to write the recordtime to a file (I know how to do this).
......
When I debug, stat(filename, buf) was successeed, but strcpy(recordtime, buf->st_mttime) was failed. Any suggestions? Thanks a lot.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks everyone.
In dos ,you can simply use ctime:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t;
time(&t);
printf("Today's date and time: %s\n", ctime(&t));
return 0;
}
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t t;
time(&t);
printf("Today's date and time: %s\n", ctime(&t));
return 0;
}
ASKER
ankuratvb, I question is how to read the properties of a file, such as its created time, modified time, et al, rather than read the current CPU time. But thank you for the help all the same.
Hi yangye,
I guess my example got you there.
In the assisted solution,the time used is of the type time_t
>time_t test;
ctime is a function that converts a time_t type to string so you can convert your date-time of creation of ur file into string using ctime().
Thats all.ctime() just simplifies your conversion of time to string.
replace:
>time_string = sprintf("%04d%02d%02d %02d:%02d:%02d", >test.tm_year + 1900, test.tm_mon+1, test.tm_mday, >test.tm_hour, test.tm_min, test.tm_sec);
with
time_string=ctime(&test);
I guess my example got you there.
In the assisted solution,the time used is of the type time_t
>time_t test;
ctime is a function that converts a time_t type to string so you can convert your date-time of creation of ur file into string using ctime().
Thats all.ctime() just simplifies your conversion of time to string.
replace:
>time_string = sprintf("%04d%02d%02d %02d:%02d:%02d", >test.tm_year + 1900, test.tm_mon+1, test.tm_mday, >test.tm_hour, test.tm_min, test.tm_sec);
with
time_string=ctime(&test);
ASKER
Thanks ankuratvb. You are right. I can use your second comment as an alternative to convert the time to string. I would like to accept your comment as another assistant answer, but since I had accepted answers yesterday, so I have used up the point and couldn't do that again. Sorry about that and thank you for all of your help.
Glad to be of help....
GetFileAttributes()
or
GetFileAttributesEx()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/getfileattributesex.asp
GetFileAttributesEx
Gives more fine grained information.
All of the file management functions are at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/file_management_functions.asp
Hope this helps
- Tim