MrSnood
asked on
GetCreationTime, GetLastAccessTime, GetLastWriteTime
My code is dying at run-time. The output displays the filename but nothing else (it bombs after there). What am I doing wrong? (Note: I'm looking for the simplest solution that builds upon this little block below, which is merely attempting to display the filename, and related time information). Thanks.
#include <afx.h>
#include <iostream>
using namespace std;
void main () {
CFileFind finder;
CTime creation_time;
BOOL bWorking = finder.FindFile("C:\\send. bat");
while (bWorking)
{
bWorking = finder.FindNextFile();
cout << (LPCTSTR) finder.GetFileName() << endl;
cout << (LPCTSTR) finder.GetCreationTime(cre ation_time ) << endl;
cout << (LPCTSTR) finder.GetLastAccessTime(c reation_ti me) << endl;
cout << (LPCTSTR) finder.GetLastWriteTime(cr eation_tim e) << endl;
}
}
#include <afx.h>
#include <iostream>
using namespace std;
void main () {
CFileFind finder;
CTime creation_time;
BOOL bWorking = finder.FindFile("C:\\send.
while (bWorking)
{
bWorking = finder.FindNextFile();
cout << (LPCTSTR) finder.GetFileName() << endl;
cout << (LPCTSTR) finder.GetCreationTime(cre
cout << (LPCTSTR) finder.GetLastAccessTime(c
cout << (LPCTSTR) finder.GetLastWriteTime(cr
}
}
The GetCreationTime() method returns a bool, and not a CString. You are casting the bool to a LPCTSTR, which of course points to the memory address 0 or 1. That's the reason you are getting a crash.
ASKER
OK, thank you, but how do I cout the stuff I'm actually looking for (the date/times)?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Snood..
Check this example...
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("C:\\*. *"));
while (bWorking)
{
bWorking = finder.FindNextFile();
_tprintf(_T("%s\n\t"), (LPCTSTR) finder.GetFileName());
_tprintf(_T("%c"), finder.IsArchived() ? 'A' : 'a');
_tprintf(_T("%c"), finder.IsCompressed() ? 'C' : 'c');
_tprintf(_T("%c"), finder.IsHidden() ? 'H' : 'h');
_tprintf(_T("%c"), finder.IsNormal() ? 'N' : 'n');
_tprintf(_T("%c"), finder.IsReadOnly() ? 'R' : 'r');
_tprintf(_T("%c"), finder.IsSystem() ? 'S' : 's');
_tprintf(_T("%c"), finder.IsTemporary() ? 'T' : 't');
_tprintf(_T("\t%I64u byte(s)\n"), finder.GetLength());
CTime tempTime;
CString str;
_tprintf(_T("\tCreated : "));
if (finder.GetCreationTime(te mpTime))
{
str = tempTime.Format(_T("%c"));
_tprintf(_T("%s\n"), (LPCTSTR) str);
}
else
_tprintf(_T("(unavailable) \n"));
_tprintf(_T("\tLast Access: "));
if (finder.GetLastAccessTime( tempTime))
{
str = tempTime.Format(_T("%c"));
_tprintf(_T("%s\n"), (LPCTSTR) str);
}
else
_tprintf(_T("(unavailable) \n"));
_tprintf(_T("\tLast Write : "));
if (finder.GetLastWriteTime(t empTime))
{
str = tempTime.Format(_T("%c"));
_tprintf(_T("%s\n"), (LPCTSTR) str);
}
else
_tprintf(_T("(unavailable) \n"));
_tprintf(_T("\n"));
}
Luv..
Jd
Check this example...
CFileFind finder;
BOOL bWorking = finder.FindFile(_T("C:\\*.
while (bWorking)
{
bWorking = finder.FindNextFile();
_tprintf(_T("%s\n\t"), (LPCTSTR) finder.GetFileName());
_tprintf(_T("%c"), finder.IsArchived() ? 'A' : 'a');
_tprintf(_T("%c"), finder.IsCompressed() ? 'C' : 'c');
_tprintf(_T("%c"), finder.IsHidden() ? 'H' : 'h');
_tprintf(_T("%c"), finder.IsNormal() ? 'N' : 'n');
_tprintf(_T("%c"), finder.IsReadOnly() ? 'R' : 'r');
_tprintf(_T("%c"), finder.IsSystem() ? 'S' : 's');
_tprintf(_T("%c"), finder.IsTemporary() ? 'T' : 't');
_tprintf(_T("\t%I64u byte(s)\n"), finder.GetLength());
CTime tempTime;
CString str;
_tprintf(_T("\tCreated : "));
if (finder.GetCreationTime(te
{
str = tempTime.Format(_T("%c"));
_tprintf(_T("%s\n"), (LPCTSTR) str);
}
else
_tprintf(_T("(unavailable)
_tprintf(_T("\tLast Access: "));
if (finder.GetLastAccessTime(
{
str = tempTime.Format(_T("%c"));
_tprintf(_T("%s\n"), (LPCTSTR) str);
}
else
_tprintf(_T("(unavailable)
_tprintf(_T("\tLast Write : "));
if (finder.GetLastWriteTime(t
{
str = tempTime.Format(_T("%c"));
_tprintf(_T("%s\n"), (LPCTSTR) str);
}
else
_tprintf(_T("(unavailable)
_tprintf(_T("\n"));
}
Luv..
Jd