How to get File Time in C++

Andro Miguel M. Bondoc picture Andro Miguel M. Bondoc · Mar 3, 2011 · Viewed 23k times · Source

i am having a problem in getting the File Time Creation.. could anyone know how to get the FILE time??

I got a code that gets the file name of a certain directory my problem is i dont know how to put the filetimes on each file in the directory...

here is my code:

#include <vector>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <iostream> 
#include <conio.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <time.h>
#include <exception>
#include <WinBase.h>

        using namespace std;
int ifException(string directory) throw()
    {
        DWORD returnvalue;
        returnvalue = GetFileAttributes(directory.c_str());
            if(returnvalue == ((DWORD)-1))
            {
                return 0;
            }
            else
            {   
                return 1;
            }
}



    vector <string> GetPath(string path)
    {



            char directory[9999];
            vector <string> filelist;
            string buffer;
            strcpy_s(directory,path.c_str());
            BOOL checker;




                try
                    {
                        ifException(directory);

                    }catch(int i)
                    {   
                        if (i==0)
                        {
                            _getch();
                            exit(1);
                        }
                    }
                buffer = findFileData.cFileName;
                filelist.push_back(buffer);
                checker = FindNextFile(hFind, &findFileData);
                while(checker)
                {
                    if(checker == 0)
                    {
                        filelist.resize(filelist.size());
                        return filelist;
                    }
                    checker = FindNextFile(hFind, &findFileData);
                    buffer = findFileData.cFileName;
                    filelist.push_back(buffer);;//save file list in vector
                }
                return filelist;
    }

    int main()
    {
       string path;

       path="C:\\Documents and Settings\\OJT\\My Documents\\game\\FSNPC\\*.*";// the directory
       vector <string> handler;
       handler = GetPath(path);
        for (unsigned i=1;i<handler.size()-1;i++)
        {
            cout << handler[i]<<endl;//print out the vector
        }
       _getch();
    }

Answer

CyberDem0n picture CyberDem0n · Mar 3, 2011

This is for *nix platforms.

   int stat(const char *path, struct stat *buf);
   int fstat(int fd, struct stat *buf);
   int lstat(const char *path, struct stat *buf);

       struct stat {
           dev_t     st_dev;     /* ID of device containing file */
           ino_t     st_ino;     /* inode number */
           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 ID (if special file) */
           off_t     st_size;    /* total size, in bytes */
           blksize_t st_blksize; /* blocksize for file system I/O */
           blkcnt_t  st_blocks;  /* number of 512B 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 status change */
       };