Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Loging System Time & Date Error

Loging System Time & Date Error

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpiosdebuggingquestion
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Suresh H

    Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code. outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close(); and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.

    N Offline
    N Offline
    Naveen
    wrote on last edited by
    #2

    Suresh H wrote:

    ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);

    this message is used to get time or date from a datetime control. Not from other windows. You should use GetLocalTime to get the current time.

    Suresh H wrote:

    new TCHAR[21];

    this buffer is too small. try a value of 50

    nave

    S D 2 Replies Last reply
    0
    • S Suresh H

      Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code. outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close(); and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.

      D Offline
      D Offline
      Don Box
      wrote on last edited by
      #3

      U can use GetLocalTime (). This API is very easier to use.

      Come online at:- jubinc@skype

      S 1 Reply Last reply
      0
      • S Suresh H

        Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code. outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close(); and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.

        K Offline
        K Offline
        kasturi_haribabu
        wrote on last edited by
        #4

        SYSTEMTime is for use with GetSystemTime() function. Example : 1. SYSTEMTIME stSystime; GetSystemTime(&stSystime); This will load UTC time into stSystime variable. 2. SYSTEMTIME stSystime; GetLocalTime(&stSystime); 3. Your Code with Local time Logging: outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; // Add this line GetLocalTime(&stSystime); //Remove this line //::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();

        S 1 Reply Last reply
        0
        • N Naveen

          Suresh H wrote:

          ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);

          this message is used to get time or date from a datetime control. Not from other windows. You should use GetLocalTime to get the current time.

          Suresh H wrote:

          new TCHAR[21];

          this buffer is too small. try a value of 50

          nave

          S Offline
          S Offline
          Suresh H
          wrote on last edited by
          #5

          Thanks Naveen for the response and the Information its very useful.

          1 Reply Last reply
          0
          • D Don Box

            U can use GetLocalTime (). This API is very easier to use.

            Come online at:- jubinc@skype

            S Offline
            S Offline
            Suresh H
            wrote on last edited by
            #6

            Thanks Don , I will make use of it .

            1 Reply Last reply
            0
            • K kasturi_haribabu

              SYSTEMTime is for use with GetSystemTime() function. Example : 1. SYSTEMTIME stSystime; GetSystemTime(&stSystime); This will load UTC time into stSystime variable. 2. SYSTEMTIME stSystime; GetLocalTime(&stSystime); 3. Your Code with Local time Logging: outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; // Add this line GetLocalTime(&stSystime); //Remove this line //::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close();

              S Offline
              S Offline
              Suresh H
              wrote on last edited by
              #7

              Thanks Babu, Your code is working … I made changes to my code its working as expected. Thank you very much ….:)

              1 Reply Last reply
              0
              • S Suresh H

                Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code. outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close(); and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #8

                Suresh H wrote:

                _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond);

                Why not put a breakpoint on this statement to verify the values of st before bothering to write them to the file? The "crash" is happening because you are writing 36 bytes into a buffer that can only hold 21.


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                "Judge not by the eye but by the heart." - Native American Proverb

                S 1 Reply Last reply
                0
                • N Naveen

                  Suresh H wrote:

                  ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st);

                  this message is used to get time or date from a datetime control. Not from other windows. You should use GetLocalTime to get the current time.

                  Suresh H wrote:

                  new TCHAR[21];

                  this buffer is too small. try a value of 50

                  nave

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #9

                  Naveen R wrote:

                  this buffer is too small.

                  How so?


                  "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                  "Judge not by the eye but by the heart." - Native American Proverb

                  N 1 Reply Last reply
                  0
                  • D David Crow

                    Naveen R wrote:

                    this buffer is too small.

                    How so?


                    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                    "Judge not by the eye but by the heart." - Native American Proverb

                    N Offline
                    N Offline
                    Naveen
                    wrote on last edited by
                    #10

                    sorry i dont understand your question. What I mean is a buffer of 21 will not be enough for a statement like _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); So i said him to increase the buffer. I found you too suggested the same to him. Then why a question like this?

                    nave

                    D 1 Reply Last reply
                    0
                    • D David Crow

                      Suresh H wrote:

                      _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond);

                      Why not put a breakpoint on this statement to verify the values of st before bothering to write them to the file? The "crash" is happening because you are writing 36 bytes into a buffer that can only hold 21.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                      "Judge not by the eye but by the heart." - Native American Proverb

                      S Offline
                      S Offline
                      Suresh H
                      wrote on last edited by
                      #11

                      Hi David, Thank you very much for the explaining the cause of crash. I had actually used break points to check the crash , I found it was cashing at this statement delete []lpDateTime; and also I was not getting the proper out put I had commented the delete statement and I was concentrating on the GetLocalTime function once I got the proper time and date later I checked the cause and I found that I had used 21 bytes instead of 36 bytes. Now I got the solution for the crash. Thanking you.,:) Suresh HC

                      D 1 Reply Last reply
                      0
                      • S Suresh H

                        Hi David, Thank you very much for the explaining the cause of crash. I had actually used break points to check the crash , I found it was cashing at this statement delete []lpDateTime; and also I was not getting the proper out put I had commented the delete statement and I was concentrating on the GetLocalTime function once I got the proper time and date later I checked the cause and I found that I had used 21 bytes instead of 36 bytes. Now I got the solution for the crash. Thanking you.,:) Suresh HC

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        Suresh H wrote:

                        I found it was cashing at this statement delete []lpDateTime;

                        And rightly so since lpDateTime had been stepped all over. The array is indeed large enough; you just need to verify that you're not trying to stuff too much into it.


                        "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                        "Judge not by the eye but by the heart." - Native American Proverb

                        1 Reply Last reply
                        0
                        • N Naveen

                          sorry i dont understand your question. What I mean is a buffer of 21 will not be enough for a statement like _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); So i said him to increase the buffer. I found you too suggested the same to him. Then why a question like this?

                          nave

                          D Offline
                          D Offline
                          David Crow
                          wrote on last edited by
                          #13

                          Naveen R wrote:

                          What I mean is a buffer of 21 will not be enough for a statement like _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond);

                          Sure it is. Actually, only 20 bytes are needed, assuming that the values in st are correct (which they weren't in his example).

                          Naveen R wrote:

                          I found you too suggested the same to him.

                          I never indicated the buffer was too small. I told him to fix the problem itself, not something unrelated further downstream.


                          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                          "Judge not by the eye but by the heart." - Native American Proverb

                          1 Reply Last reply
                          0
                          • S Suresh H

                            Hello all, I am trying to log the system date and time in the log file in my win 32 application. I have used below code. outf.open("C:\\Log.txt",ios::app); SYSTEMTIME st; ::SendMessage(hwnd, DTM_GETSYSTEMTIME, 0, (LPARAM)&st); LPTSTR lpDateTime = new TCHAR[21]; _stprintf(lpDateTime, _T("%02u/%02u/%04u %2u:%2u:%2u"), st.wMonth, st.wDay, st.wYear,st.wHour, st.wMinute, st.wSecond); outf << "Current selected time: " << lpDateTime; delete []lpDateTime; outf.close(); and I am getting out put as :- Current selected time: 52428/52428/52428 52428:52428:52428 Can anyone help me with this why I am not getting the proper time and date ??? And also this code has no compiler errors I am getting run time error for this line delete []lpDateTime; debug error :- Microsoft Visual C++ Debug Library Debug Error ! Damage : after normal block (# 45) at 0X0071DAO Why I am getting the debug error ???? delete []lpDateTime; is correct way right ?? Thanking you, Suresh HC.

                            H Offline
                            H Offline
                            Hamid Taebi
                            wrote on last edited by
                            #14

                            And see Date and Time in C++[^] ;)


                            WhiteSky


                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups