comparing char buffer to string not working

When trying to compare a char buffer to a std string in an if statement it’s not working as intended Here is the code

if (ConnectNamedPipe(hPipe, NULL) != FALSE)   // wait for someone to connect to the pipe
    {
        while (ReadFile(hPipe, buffer, sizeof(buffer) - 1, &dwRead, NULL) != FALSE)
        {
            /* add terminating zero */
            buffer[dwRead] = 0x00;

            /* do something with data in buffer */
            printf("%sn", buffer);
            
            string cmd = bufferToString(buffer, sizeof(buffer));

            printf("%s", cmd.c_str());

            if (cmd.c_str() == "help") //HERE is the issue
            {
                printf("hello");
            }

        }
    }

When comparing it doesn’t work I’ve tried using different types of conversions of char buffer[1024] to a string but not getting anywhere

EDIT: I’ve tried so far

    cmd.resize(dwRead);
if (cmd == string("help")) 

and

if (0 == ::std::strcmp(buffer, "help"))

none of them work

Answer

Instead of reading the data into a char[] and then copying that data into a std::string, you could use a std::string directly.

Building blocks:

std::string cmd;

cmd.resize(wanted_buffer_size); // Set the proper buffer size

ReadFile(hPipe, cmd.data(), cmd.size(), &dwRead, nullptr); // Read directly into cmd

cmd.resize(dwRead); // Shrink down to dwRead afterwards. Automatically null terminated.