title: C++ beginner how to use GetSystemTimeAsFileTime
body: <p>I have a program that reads the current time from the system clock and saves it to a text file. I previously used the GetSystemTime function which worked, but the times weren't completely consistent eg: one of the times is 32567.789 and the next time is 32567.780 which is backwards in time. </p>
  
  <p>I am using this program to save the time up to 10 times a second. I read that the GetSystemTimeAsFileTime function is more accurate. My question is, how to I convert my current code to use the GetSystemTimeAsFileTime function? I tried to use the FileTimeToSystemTime function but that had the same problems. </p>
  
  <pre><code>SYSTEMTIME st;
  GetSystemTime(&amp;st);
  
  WORD sec = (st.wHour*3600) + (st.wMinute*60) + st.wSecond; //convert to seconds in a day
  lStr.Format( _T("%d   %d.%d\n"),GetFrames() ,sec, st.wMilliseconds);
  
  std::wfstream myfile;  
  myfile.open("time.txt", std::ios::out | std::ios::in | std::ios::app );
  if (myfile.is_open())
      {
       myfile.write((LPCTSTR)lStr, lStr.GetLength());
       myfile.close();
      }
  else {lStr.Format( _T("open file failed: %d"), WSAGetLastError());
  }           
  </code></pre>
  
  <p><strong>EDIT</strong> To add some more info, the code captures an image from a camera which runs 10 times every second and saves the time the image was taken into a text file. When I subtract the 1st entry of the text file from the second and so on eg: entry 2-1 3-2 4-3 etc I get this graph, where the x axis is the number of entries and the y axis is the subtracted values. </p>
  
  <p><a href="https://i.stack.imgur.com/pSrEQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pSrEQ.png" alt="enter image description here"></a></p>
  
  <p>All of them should be around the 0.12 mark which most of them are. However you can see that a lot of them vary and some even go negative. This isn't due to the camera because the camera has its own internal clock and that has no variations. It has something to do with capturing the system time. What I want is the most accurate method to extract the system time with the highest resolution and as little noise as possible. </p>
  
  <p><strong>Edit 2</strong> I have taken on board your suggestions and ran the program again. This is the result:</p>
  
  <p><a href="https://i.stack.imgur.com/DUhHA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DUhHA.png" alt="enter image description here"></a>      </p>
  
  <p>As you can see it is a lot better than before but it is still not right. I find it strange that it seems to do it very incrementally. I also just plotted the times and this is the result, where x is the entry and y is the time: </p>
  
  <p><a href="https://i.stack.imgur.com/SmMyH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SmMyH.png" alt="enter image description here"></a>              </p>
  
  <p>Does anyone have any idea on what could be causing the time to go out every 30 frames or so?</p>