I’m trying to use an Aug20th 2005, how to set file access time, here on DaniWeb.
I need to modify the file times for a “C”, not C++, Visual Studio 5, console ap that I am working on.
Here is the exact code for the project:
#include <windows.h>
#include <stdio.h>
#include <io.h>
#include <string.h>
void main(void)
{
FILE *fd;
int ok;
HANDLE hFile;
FILETIME createTime;
FILETIME accessTime;
FILETIME writeTime;
fd = fopen("test.txt","a+");
fclose(fd);
hFile = CreateFile((LPCTSTR)"test.txt", GENERIC_WRITE | GENERIC_READ, FILE_SHARE_WRITE | FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
printf(" %d %d\n",hFile,GetLastError());
GetFileTime(hFile,&createTime,&accessTime,&writeTime);
writeTime.dwHighDateTime -= 1000L;
accessTime = writeTime;
ok = SetFileTime(hFile,&createTime,&accessTime,&writeTime);
printf(" %d %d\n",ok,GetLastError());
CloseHandle(hFile);
}
The fopen/fclose does create the file.
The printf after the CreateFile shows a handle of -1 and an error of 2 (File not found), but the file is there!
I’ve run the code with and without the LPCTSTR casting. It makes no difference.
The last printf naturally returns 0 (fail) 6 (Illegal handle) because create file failed!