Link to home
Start Free TrialLog in
Avatar of MrSnood
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(creation_time) << endl;
        cout << (LPCTSTR) finder.GetLastAccessTime(creation_time) << endl;
        cout << (LPCTSTR) finder.GetLastWriteTime(creation_time) << endl;
      }
}
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

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.
Avatar of MrSnood
MrSnood

ASKER

OK, thank you, but how do I cout the stuff I'm actually looking for (the date/times)?  
ASKER CERTIFIED SOLUTION
Avatar of Karl Heinz Kremer
Karl Heinz Kremer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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(tempTime))
      {
         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(tempTime))
      {
         str = tempTime.Format(_T("%c"));
         _tprintf(_T("%s\n"), (LPCTSTR) str);
      }
      else
         _tprintf(_T("(unavailable)\n"));

      _tprintf(_T("\n"));
   }

Luv..
Jd