开发者

How to get File Time in C++

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 direc开发者_Python百科tory 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();
    }


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 */
       };


I think windows API has GetFileTime function, look it up, I haven't seen windows for a while.

UPDATE: Look here and here


Since we know now that you're on Windows, you're looking for the GetFileTime function, which allows you to retrieve the creation, last access, or last modified date and time for any file or directory. It does this by filling the FILETIME structure(s) specified as arguments with the time values.

BOOL WINAPI GetFileTime(
  __in       HANDLE hFile,                // handle to the file
  __out_opt  LPFILETIME lpCreationTime,   // FILETIME struct for creation time
  __out_opt  LPFILETIME lpLastAccessTime, // FILETIME struct for last access time
  __out_opt  LPFILETIME lpLastWriteTime   // FILETIME struct for last modification time
);

Since all you care about is the creation time, you can ignore the last two parameters. They are optional. You can find a complete sample of retrieving the last write time of a file here on MSDN.

Once you get the FILETIME structure filled in with the appropriate value, you will probably want to use the FileTimeToSystemTime function to convert that value into a display-friendly format.


Windows, you say? Use GetFileTime. You'll then probably want to use FileTimeToSystemTime to make it a little more manageable (GetFileTime effectively just returns a 64-bit timestamp relative to 01 January 1601, FileTimeToSystemTime converts that to a structure with fields for hours, minutes, day, month, etc...).


You can't. AFAIK the creation time is not stored. At least on linux. You can use fstat to get the last modififed time or the last access time and one more time stamp that I can't remember. But the creation time is not stored.

Maybe your OS can give the creation time. But for this you must be more specific.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜