home *** CD-ROM | disk | FTP | other *** search
/ Australian Personal Computer 2000 November / APC5112K.ISO / workshop / c / datestamp.cpp next >
Encoding:
C/C++ Source or Header  |  2000-08-27  |  902 b   |  55 lines

  1. #pragma warning( disable:4786 )
  2. #include <iostream>
  3. #include <fstream>
  4. #include <sstream>
  5. #include <string>
  6. #include <ctime>
  7.  
  8. using namespace std;
  9.  
  10. void getDate( int &day, 
  11.               int &month, 
  12.               int &year )
  13. {
  14.    time_t now = time(NULL);
  15.    struct tm *pTm = 
  16.         localtime( &now );
  17.    day   = pTm->tm_mday;
  18.    month = pTm->tm_mon;
  19.    year  = pTm->tm_year+1900;
  20. }
  21.  
  22. string makeFileName()
  23. {
  24.    int day, month, year;
  25.    getDate(day,month,year);
  26.  
  27.    ostringstream os;
  28.  
  29.    os << day
  30.       << "-"
  31.       << month
  32.       << "-"
  33.       << year
  34.       << ".log";
  35.    return os.str();
  36. }
  37.  
  38.  
  39. void writeLog( const string &s )
  40. {
  41.    string fname = 
  42.          makeFileName();
  43.    ofstream of( 
  44.              fname.c_str(), 
  45.              ios::app );
  46.    if (of.is_open())
  47.       of << s << endl;
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53.    writeLog( "Something" );
  54.    return 0;
  55. }