Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to read file properties in C ?

Posted on 2004-04-21
Medium Priority
Last Modified: 2008-02-01
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.
Question by:yangye
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1

Accepted Solution

cameron_schuler earned 150 total points
ID: 10882868
The following is from  a man page:

       stat, fstat, lstat - get file status

       #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>

       int stat(const char *file_name, struct stat *buf);
       int fstat(int filedes, struct stat *buf);
       int lstat(const char *file_name, struct stat *buf);

       These  functions  return  information  about the specified
       file.  You do not need any access rights to  the  file  to
       get  this  information  but  you need search rights to all
       directories named in the path leading to the file.

       stat stats the file pointed to by file_name and  fills  in

       lstat  is  identical to stat, except in the case of a sym­
       bolic link, where the link itself is stat-ed, not the file
       that it refers to.

       fstat  is identical to stat, only the open file pointed to
       by filedes (as returned by open(2)) is stated in place  of

       They  all return a stat structure, which contains the fol­
       lowing fields:

              struct stat {
                  dev_t         st_dev;      /* device */
                  ino_t         st_ino;      /* inode */
                  mode_t        st_mode;     /* protection */
                  nlink_t       st_nlink;    /* number of hard links */
                  uid_t         st_uid;      /* user ID of owner */
                  gid_t         st_gid;      /* group ID of owner */
                  dev_t         st_rdev;     /* device type (if inode device) */                  off_t         st_size;     /* total size, in bytes */
                  unsigned long st_blksize;  /* blocksize for filesystem I/O */
                  unsigned long st_blocks;   /* number of blocks allocated */
                  time_t        st_atime;    /* time of last access */
                  time_t        st_mtime;    /* time of last modification */
                  time_t        st_ctime;    /* time of last change */

       The value st_size gives the size of the file (if it  is  a
       regular file or a symlink) in bytes. The size of a symlink
       is the length of the pathname it contains, without  trail­
       ing NUL.

       The value st_blocks gives the size of the file in 512-byte
       blocks.  (This may be smaller than st_size/512  e.g.  when
       the file has holes.)  The value st_blksize gives the "pre­
       ferred" blocksize for efficient file system I/O.  (Writing
       to a file in smaller chunks may cause an inefficient read-

       Not all of the Linux filesystems implement all of the time
       fields.   Some  file system types allow mounting in such a
       way that file accesses do  not  cause  an  update  of  the
       st_atime field. (See `noatime' in mount(8).)

       The  field  st_atime  is changed by file accesses, e.g. by
       exec(2), mknod(2), pipe(2), utime(2) and read(2) (of  more
       than zero bytes). Other routines, like mmap(2), may or may
       not update st_atime.

       The field st_mtime is changed by file modifications,  e.g.
       by  mknod(2),  truncate(2), utime(2) and write(2) (of more
       than zero bytes).  Moreover, st_mtime of  a  directory  is
       changed  by  the  creation  or  deletion  of files in that
       directory.  The st_mtime field is not changed for  changes
       in owner, group, hard link count, or mode.

       The  field  st_ctime  is  changed by writing or by setting
       inode information (i.e., owner, group, link  count,  mode,

       The  following  POSIX macros are defined to check the file

              S_ISREG(m)  is it a regular file?

              S_ISDIR(m)  directory?

              S_ISCHR(m)  character device?

              S_ISBLK(m)  block device?

              S_ISFIFO(m) fifo?

              S_ISLNK(m)  symbolic link? (Not in POSIX.1-1996.)

              S_ISSOCK(m) socket? (Not in POSIX.1-1996.)


Expert Comment

ID: 10883199
If you want to do this in Windows you probably want

Gives more fine grained information.

All of the file management functions are at:

Hope this helps
- Tim

Assisted Solution

timbauer earned 60 total points
ID: 10883280
//Here is some code to get you started

#include <windows.h>

int main( int argc, char **argv )
  //info on this struct is at
  if( argc<2 ){
    printf("need a filename\n");
    return 1;

  printf("Getting info on file: %s\n",argv[1] );
  if( !GetFileAttributesEx( argv[1], GetFileExInfoStandard, &info ) )
    printf("ERROR code %ld\n", GetLastError() );
    return 1;
  { // the "directory" bit is set.
    printf(" file is a directory\n");
  } else {
    printf(" file is not a directory and is %d bytes\n", info.nFileSizeLow );

  return 0;
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 10883412
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.

Assisted Solution

cameron_schuler earned 150 total points
ID: 10883495
I would just use something like:

char time_string[32];
time_t test;

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);

Use sprintf to put the time in a string in the format of your liking then you can use that string to put in the file.

Author Comment

ID: 10884152
Thanks everyone.

Expert Comment

ID: 10885794
In dos ,you can simply use ctime:

#include <stdio.h>
#include <time.h>

int main(void)
   time_t t;
   printf("Today's date and time: %s\n", ctime(&t));
   return 0;

Author Comment

ID: 10888624
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.

Expert Comment

ID: 10888931
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.


>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);



Author Comment

ID: 10890519
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.

Expert Comment

ID: 10890549
Glad to be of help....

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question