2014-02-24 50 views
0

我试图从文件中读取创建日期,将其存储在一个字符串中,然后将其输出到格式化的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); 

如果有人能指出我要去哪里错了,或者给我一个简单版本的“获取文件的日期,并将其存储在字符数组”,的老年大加赞赏。

由于 理查德

+0

sAdatetext = sAdatestring.c_str(); // sAdatestring获取数据在哪里? – Jagannath

回答

0

既然你说C++ 11,使用的FileHandle从wrl.h

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

}

相关问题