Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading oldest file

Status
Not open for further replies.

logic4fun

Programmer
Apr 24, 2003
85
US
All,
I have a C program with embedded SQL which reads the files and loads them into database. Today i am using a system call to ls -lrt and taking the output into an array to get my oldest file. I am trying to avoid using system call. Is there anyway to retrieve the oldest file from the directory and get it processed.

thanks for your time
logic4fun
 
Use opendir() / readddir() / closedir() to access all the files in a directory.
Use stat() to get the information on each file.

You pass a pointer to one of these
Code:
              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 */
                  blksize_t     st_blksize;  /* blocksize for filesystem I/O */
                  blkcnt_t      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 */
              };
and stat() fills it in with information about a file.
The st_mtime is the one you need to be looking at, find the one with the smallest (ie oldest) value.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top