Board index » cppbuilder » FILETIME doesn't return valid data [or any data]

FILETIME doesn't return valid data [or any data]


2008-02-25 09:08:57 AM
cppbuilder91
Winxp, CG2007 [all upds]...C++
I get no results from the calls to getfiletime -- return value is zero,
which indicates success...but other conversion don't work...
any thoughts will be gratefully appreciated... fTime basically comes back
with '0/0/1' or '0/0/1601'...thanks.
String file = "D:\\PLANFAX\\EXE\\SysAllocationReportForOne.exe";
char fTime[132];
FILETIME creation, access, write;
//Directory of c:\planfax\exe
if (FileExists(file))
{
int rc=GetFileTime(file.c_str(), &creation, &access, &write);
TDateTime fCreation;
SYSTEMTIME stUTC, stLocal;
DWORD dwRet;
unsigned short datex, timex;
// Convert the create time to local time.
rc = FileTimeToDosDateTime(&creation, &datex, &timex);
rc = FileTimeToSystemTime(&creation, &stUTC);
rc = SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
int dwSize = 132;
// Build a string showing the date and time.
dwRet = StringCchPrintf(fTime, dwSize,
TEXT("%02d/%02d/%d %02d:%02d"),
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);
}
 
 

Re:FILETIME doesn't return valid data [or any data]

OOPS!!! needed file HANDLE!!!! DUHHHH!!!
HANDLE hFile = CreateFile(TEXT(file.c_str()), // file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open file (error %d)\n", GetLastError());
return ;
} int rc=GetFileTime(hFile, &creation, &access, &write);
TDateTime fCreation;
SYSTEMTIME stUTC, stLocal;
DWORD dwRet;
unsigned short datex, timex;
// Convert the last-write time to local time.
rc = FileTimeToDosDateTime(&creation, &datex, &timex);
rc = FileTimeToSystemTime(&creation, &stUTC);
rc = SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
int dwSize = 132;
// Build a string showing the date and time.
dwRet = StringCchPrintf(fTime, dwSize,
TEXT("%02d/%02d/%d %02d:%02d"),
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);
"ActuaryOne" < XXXX@XXXXX.COM >wrote in message
Quote
Winxp, CG2007 [all upds]...C++
I get no results from the calls to getfiletime -- return value is zero,
which indicates success...but other conversion don't work...
any thoughts will be gratefully appreciated... fTime basically comes back
with '0/0/1' or '0/0/1601'...thanks.

String file = "D:\\PLANFAX\\EXE\\SysAllocationReportForOne.exe";
char fTime[132];
FILETIME creation, access, write;

//Directory of c:\planfax\exe
if (FileExists(file))
{
int rc=GetFileTime(file.c_str(), &creation, &access, &write);
TDateTime fCreation;
SYSTEMTIME stUTC, stLocal;
DWORD dwRet;
unsigned short datex, timex;
// Convert the create time to local time.
rc = FileTimeToDosDateTime(&creation, &datex, &timex);
rc = FileTimeToSystemTime(&creation, &stUTC);
rc = SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
int dwSize = 132;
// Build a string showing the date and time.
dwRet = StringCchPrintf(fTime, dwSize, TEXT("%02d/%02d/%d
%02d:%02d"),
stLocal.wMonth, stLocal.wDay,
stLocal.wYear,
stLocal.wHour, stLocal.wMinute);
}


 

Re:FILETIME doesn't return valid data [or any data]

When posting code, please post what you actually used but check to include
all variable declarations.
I was kind of confused by the variables with no declaration, assignment of
return values to an otherwise unused variable and failure to close an open
file but after beating it up a bit, the code worked. I don't see a problem.
I used a simple console mode program. The code and a screen capture are
below.
. Ed
---------------------------------------
#include <windows.h>
#include <strsafe.h>
#include <stdio.h>
LPSTR WINAPI TestFileTime(const char *name)
{
HANDLE hFile =
CreateFile(
name, // name of file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if (hFile == INVALID_HANDLE_VALUE)
{
return "Could not open file";
}
// unused TDateTime fCreation;
SYSTEMTIME stUTC;
SYSTEMTIME stLocal;
// unused DWORD dwRet;
unsigned short datex;
unsigned short timex;
FILETIME creation; // added
FILETIME access; // added
FILETIME write; // added
// unused int rc
GetFileTime(hFile, &creation, &access, &write);
// Convert the last-write time to local time.
FileTimeToDosDateTime(&creation, &datex, &timex);
FileTimeToSystemTime(&creation, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
CloseHandle(hFile); // added
static char fTime[132]; // added
int dwSize = 132;
// Build a string showing the date and time.
// dwRet =
StringCchPrintf(
fTime,
dwSize,
TEXT("%02d/%02d/%d %02d:%02d"),
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);
return fTime;
}
const char filename[] = "somename.txt";
int main()
{
FILE *fptr = fopen(filename, "wb");
if (fptr)
{
fputs("contents of file\r\n", fptr);
fclose(fptr);
puts(TestFileTime(filename));
}
else
puts("Could not create file");
return 0;
}
---------------------------------------
C:\Documents and Settings\Edward\My Documents\lookat\q242
Quote
dir
Volume in drive C has no label.
Volume Serial Number is FC8D-A209
Directory of C:\Documents and Settings\Edward\My Documents\lookat\q242
02/25/2008 12:45 AM <DIR>.
02/25/2008 12:45 AM <DIR>..
02/25/2008 12:44 AM 1,769 ques242.cpp
02/25/2008 12:45 AM 7,680 ques242.exe
02/25/2008 12:45 AM 7,596 ques242.obj
02/25/2008 12:45 AM 65,536 ques242.tds
02/25/2008 12:45 AM 18 somename.txt
5 File(s) 82,599 bytes
2 Dir(s) 27,816,988,672 bytes free
C:\Documents and Settings\Edward\My Documents\lookat\q242
Quote

---------------------------------------
Quote
ActuaryOne wrote in message
news:47c21527$ XXXX@XXXXX.COM ...

Winxp, CG2007 [all upds]...C++
I get no results from the calls to getfiletime -- return value is zero,
which indicates success...but other conversion don't work...
any thoughts will be gratefully appreciated... fTime basically comes back
with '0/0/1' or '0/0/1601'...thanks.

String file = "D:\\PLANFAX\\EXE\\SysAllocationReportForOne.exe";
char fTime[132];
FILETIME creation, access, write;

//Directory of c:\planfax\exe
if (FileExists(file))
{
int rc=GetFileTime(file.c_str(), &creation, &access, &write);
TDateTime fCreation;
SYSTEMTIME stUTC, stLocal;
DWORD dwRet;
unsigned short datex, timex;
// Convert the create time to local time.
rc = FileTimeToDosDateTime(&creation, &datex, &timex);
rc = FileTimeToSystemTime(&creation, &stUTC);
rc = SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
int dwSize = 132;
// Build a string showing the date and time.
dwRet = StringCchPrintf(fTime, dwSize, TEXT("%02d/%02d/%d
%02d:%02d"),
stLocal.wMonth, stLocal.wDay,
stLocal.wYear,
stLocal.wHour, stLocal.wMinute);
}
 

{smallsort}

Re:FILETIME doesn't return valid data [or any data]

"ActuaryOne" < XXXX@XXXXX.COM >wrote in message
Quote
I get no results from the calls to getfiletime -- return value
is zero, which indicates success...
No, it does not. GetFileTime() returns a BOOL, so a value of 0 is FALSE,
meaning failure.
Quote
any thoughts will be gratefully appreciated... fTime basically
comes back with '0/0/1' or '0/0/1601'...thanks.
Because GetFileTime() is failing, you are converting garbage into more
garbage.
Gambit
 

Re:FILETIME doesn't return valid data [or any data]

Ed,
This is totally off topic, and you can consign it to the bit bucket, but
I just wanted to say hello.
Brian Withnell (emeritus)
Ed Mulroy [TeamB] wrote:
Quote
When posting code, please post what you actually used but check to include
all variable declarations.

I was kind of confused by the variables with no declaration, assignment of
return values to an otherwise unused variable and failure to close an open
file but after beating it up a bit, the code worked. I don't see a problem.

I used a simple console mode program. The code and a screen capture are
below.

. Ed

---------------------------------------
#include <windows.h>
#include <strsafe.h>
#include <stdio.h>

LPSTR WINAPI TestFileTime(const char *name)
{
HANDLE hFile =
CreateFile(
name, // name of file to open
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template

if (hFile == INVALID_HANDLE_VALUE)
{
return "Could not open file";
}

// unused TDateTime fCreation;
SYSTEMTIME stUTC;
SYSTEMTIME stLocal;
// unused DWORD dwRet;
unsigned short datex;
unsigned short timex;

FILETIME creation; // added
FILETIME access; // added
FILETIME write; // added

// unused int rc

GetFileTime(hFile, &creation, &access, &write);

// Convert the last-write time to local time.
FileTimeToDosDateTime(&creation, &datex, &timex);
FileTimeToSystemTime(&creation, &stUTC);
SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

CloseHandle(hFile); // added

static char fTime[132]; // added

int dwSize = 132;

// Build a string showing the date and time.
// dwRet =
StringCchPrintf(
fTime,
dwSize,
TEXT("%02d/%02d/%d %02d:%02d"),
stLocal.wMonth, stLocal.wDay, stLocal.wYear,
stLocal.wHour, stLocal.wMinute);

return fTime;
}

const char filename[] = "somename.txt";

int main()
{
FILE *fptr = fopen(filename, "wb");

if (fptr)
{
fputs("contents of file\r\n", fptr);
fclose(fptr);
puts(TestFileTime(filename));
}
else
puts("Could not create file");

return 0;
}
---------------------------------------
C:\Documents and Settings\Edward\My Documents\lookat\q242
>dir
Volume in drive C has no label.
Volume Serial Number is FC8D-A209

Directory of C:\Documents and Settings\Edward\My Documents\lookat\q242

02/25/2008 12:45 AM <DIR>.
02/25/2008 12:45 AM <DIR>..
02/25/2008 12:44 AM 1,769 ques242.cpp
02/25/2008 12:45 AM 7,680 ques242.exe
02/25/2008 12:45 AM 7,596 ques242.obj
02/25/2008 12:45 AM 65,536 ques242.tds
02/25/2008 12:45 AM 18 somename.txt
5 File(s) 82,599 bytes
2 Dir(s) 27,816,988,672 bytes free

C:\Documents and Settings\Edward\My Documents\lookat\q242
---------------------------------------

>ActuaryOne wrote in message
>news:47c21527$ XXXX@XXXXX.COM ...
>
>Winxp, CG2007 [all upds]...C++
>I get no results from the calls to getfiletime -- return value is zero,
>which indicates success...but other conversion don't work...
>any thoughts will be gratefully appreciated... fTime basically comes back
>with '0/0/1' or '0/0/1601'...thanks.
>
>String file = "D:\\PLANFAX\\EXE\\SysAllocationReportForOne.exe";
>char fTime[132];
>FILETIME creation, access, write;
>
>//Directory of c:\planfax\exe
>if (FileExists(file))
>{
>int rc=GetFileTime(file.c_str(), &creation, &access, &write);
>TDateTime fCreation;
>SYSTEMTIME stUTC, stLocal;
>DWORD dwRet;
>unsigned short datex, timex;
>// Convert the create time to local time.
>rc = FileTimeToDosDateTime(&creation, &datex, &timex);
>rc = FileTimeToSystemTime(&creation, &stUTC);
>rc = SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
>int dwSize = 132;
>// Build a string showing the date and time.
>dwRet = StringCchPrintf(fTime, dwSize, TEXT("%02d/%02d/%d
>%02d:%02d"),
>stLocal.wMonth, stLocal.wDay,
>stLocal.wYear,
>stLocal.wHour, stLocal.wMinute);
>}