title: C++ windows.h WriteFile function
body: <p>I am trying to display in console a list of running processes and the current time and save them to a text file using the WriteFile and windows.h functions. How to effectively redirect the output stream and "My data..." to a text file without using "freopen" in C ++?</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;tlhelp32.h&gt;
  #include &lt;iostream&gt;
  #include &lt;chrono&gt;
  
  using namespace std;
  
  int main()
  {
      char temp;
        HANDLE h = CreateFile("process.txt",  // name of the file
                            GENERIC_WRITE,    // open for writing
                            0,                // sharing mode, none in this case
                            0,                // use default security descriptor
                            CREATE_ALWAYS,    // overwrite if exists
                            FILE_ATTRIBUTE_NORMAL,
                            0);
  
       if (h)
      {
          std::cout &lt;&lt; "CreateFile() succeeded\n";
          CloseHandle(h);
      }
      else
      {
          std::cerr &lt;&lt; "CreateFile() failed:" &lt;&lt; GetLastError() &lt;&lt; "\n";
      }
  
      time_t actualTime = chrono::system_clock::to_time_t(chrono::system_clock::now());
      cout &lt;&lt; ctime(&amp;actualTime);
      cout &lt;&lt; "My data..." &lt;&lt; endl;
  
      PROCESSENTRY32 proc32;
      HANDLE hSnapshot;
  
      hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
      proc32.dwSize = sizeof(PROCESSENTRY32);
  
     if(Process32First(hSnapshot, &amp;proc32))
     {
          cout &lt;&lt; proc32.szExeFile &lt;&lt; endl;
          while(Process32Next(hSnapshot, &amp;proc32))
          cout &lt;&lt; proc32.szExeFile &lt;&lt; endl;
     }
  
     WriteFile(HANDLE hFile,
              LPCVOID lpBuffer,
              DWORD nNumberOfBytesToWrite,
              LPDWORD lpNumberOfBytesWritten,
              LPOVERLAPPED lpOverlapped
              );
  
      CloseHandle(hSnapshot);
  
      system ("pause &gt;nul");
  
      return 0;
  }
  </code></pre>