获取文件创建日期并将其放入字符串中

时间:2014-02-24 04:50:49

标签: string datetime c++11 type-conversion

我正在尝试从文件中读取创建日期,将其存储在字符串中,然后将其输出到格式化的CSV文件中。 目前,我已经从无数不同来源汇总了代码,徒劳地希望它能做我想做的事情,但没有很好的侮辱,它没有(在1601年得到一个日期,如同显然至少有一个转换不起作用。)

我正在使用;

char        sourceAfilename[255];  // The name of the source file I want to
                                   // get the creation date of
FILE        sAptr;                 // Pointer to the source file
FILE        o1ptr;                 // Pointer to the output file
std::string sAdateString;          // date field - String   
const char* sAdateText;            // date field - char array
FILETIME    sAfileTime;            // filetime version of the date
SYSTEMTIME  sAsystemTime;           // systemtime version of the date
std::stringstream sAstringStream;   // Temp stringstream
HANDLE      sAhandle                // Handle of the File

// Open the files
// read and write stuff from/to the files

// now get the date of the source file
fileHandle = CreateFile(LPWSTR(sourceAfilename), GENERIC_READ, 0, NULL, OPEN_EXISTING, 
FILE_ATTRIBUTE_NORMAL, NULL);
GetFileTime(sAhandle, &sAfileTime, NULL, NULL);
CloseHandle(sAhandle);
FileTimeToSystemTime(&sAfileTime, sAsystemTime);
sAstringStream << sAsystemTime.wDay << '/' << sAsystemTime.wMonth << '/' << 
sAsystemTime.wYear;
sAdatetext = sAdatestring.c_str();

// Now output the text version of the date to the output file
fputs(" * Creation date is - ", o1ptr);
fputs(sAdatetext, o1ptr);
fputs(" * \n", o1ptr);

如果有人能指出我出错的地方,或者给我一个简单版本的'获取文件日期并将其存储在char数组中',那么我们将非常感激。

由于 理查德

1 个答案:

答案 0 :(得分:0)

因为您使用了wrl.h中的FileHandle

来说C ++ 11
#include <wrl.h>
#include <iostream>
#include <sstream>

namespace wrl = Microsoft::WRL::Wrappers;

int main(int argc, char* argv [])
{
    if (argc != 2)
    {
        printf("This sample takes a file name as a parameter\n");
        return 0;
    }
    wrl::FileHandle hFile (CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, 0, NULL));

if (!hFile.IsValid())
{
    printf("CreateFile failed with %d\n", GetLastError());
    return 0;
}

FILETIME sAfileTime;
SYSTEMTIME sAsystemTime, stLocal;
GetFileTime(hFile.Get(), &sAfileTime, NULL, NULL);
FileTimeToSystemTime(&sAfileTime, &sAsystemTime);
SystemTimeToTzSpecificLocalTime(NULL, &sAsystemTime, &stLocal);
std::stringstream sAstringStream;
sAstringStream << stLocal.wDay << '/' << stLocal.wMonth << '/' << stLocal.wYear;
// Now output the text version of the date to the output file
puts(sAstringStream.str().c_str());

 std::cout << "\nPress Enter to exit";
 std::cin.ignore();

}