0

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 ++?

#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
#include <chrono>

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 << "CreateFile() succeeded\n";
        CloseHandle(h);
    }
    else
    {
        std::cerr << "CreateFile() failed:" << GetLastError() << "\n";
    }

    time_t actualTime = chrono::system_clock::to_time_t(chrono::system_clock::now());
    cout << ctime(&actualTime);
    cout << "My data..." << endl;

    PROCESSENTRY32 proc32;
    HANDLE hSnapshot;

    hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    proc32.dwSize = sizeof(PROCESSENTRY32);

   if(Process32First(hSnapshot, &proc32))
   {
        cout << proc32.szExeFile << endl;
        while(Process32Next(hSnapshot, &proc32))
        cout << proc32.szExeFile << endl;
   }

   WriteFile(HANDLE hFile,
            LPCVOID lpBuffer,
            DWORD nNumberOfBytesToWrite,
            LPDWORD lpNumberOfBytesWritten,
            LPOVERLAPPED lpOverlapped
            );

    CloseHandle(hSnapshot);

    system ("pause >nul");

    return 0;
}
uhlik
  • 105
  • 9
  • 2
    Why not using `std::ofstream`? – jpo38 Jan 13 '19 at 07:24
  • What does all the process enumeration stuff have to do with redirecting output to a file? It just distracts from the question at hand. Please reduce the code to a [mcve]. – zett42 Jan 13 '19 at 11:22
  • Possible duplicate of [How to redirect cin and cout to files?](https://stackoverflow.com/questions/10150468/how-to-redirect-cin-and-cout-to-files) – Raymond Chen Jan 19 '19 at 21:09

1 Answers1

2

Use OPEN_ALWAYS instead of CREATE_ALWAYS and then use SetFilePointer to move the file pointer to the end of file.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • `SetFilePointer` not exist sense to use. need use explicit offset in `OVERLAPPED` – RbMm Jan 13 '19 at 06:53
  • You don't need asynchronous operation. – Michael Chourdakis Jan 13 '19 at 06:56
  • how is asynchronous here related ?! i say about explicit file offset. the `SetFilePointer` obsolete from begin and senseless – RbMm Jan 13 '19 at 07:00
  • `SetFilePointer` must be never used at all - senseless api. so advice use it bad. use `OVERLAPPED` not mean use asynchronous I/O – RbMm Jan 13 '19 at 07:07
  • and why *Use `OPEN_ALWAYS` instead of `CREATE_ALWAYS`* ? op want *overwrite if exists* - so he correct use `CREATE_ALWAYS` option. the `OPEN_ALWAYS` is wrong here – RbMm Jan 13 '19 at 07:16
  • Nothing wrong with `SetFilePointer`. Not everyone is a 1337 kernel hacker. – Jonathan Potter Jan 13 '19 at 07:16
  • @JonathanPotter - how this is related to kernel and hacking ? `SetFilePointer` is not wrong formally but senseless. for what need heavy additional call to kernel, when we can explicity set file offset in io operation ? for what even from src code view have 2 api calls instead 1 ? – RbMm Jan 13 '19 at 07:18
  • [*The old DOS SetFilePointer API is an anachronism. One should specify the file offset in the overlapped structure even for synchronous I/O.*](https://blogs.technet.microsoft.com/winserverperformance/2008/06/25/designing-applications-for-high-performance-part-iii/) – RbMm Jan 13 '19 at 07:36
  • There is nothing wrong with SetFilePointer, it's MSDN documentation that counts, not what users think. – Michael Chourdakis Jan 13 '19 at 08:31
  • @Michael - i not say that this is *wrong* api. i say that this is *anachronism* api. for what need this additional api call ? when we can simply not use it ? question is next - for what use something, which is not need ? only take time and place – RbMm Jan 13 '19 at 10:06