0

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("%s\n", 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

1

4 Answers 4

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much I'm new to strings and chars in c++ and this has helped a ton
@DavidTheTech Glad to hear it! You're welcome!
0

You are creating string copying sizeof(buffer) characters while it only contains dwRead of initialized characters.

Then you compare two pointers instead of string content (which still won't work because cmd has wrong length). You should actually compare using strcmp without creating temporary std::string object.

if (0 == ::std::strcmp(buffer, "help"))
{
    printf("hello");
}

Comments

0

You should either do

cmd.resize(dwRead);

This will set the string to the length of the actual read data, instead of the whole buffer. A c++ std::string can contain any data , including 0-bytes.

Or you must call

string cmd = bufferToString(buffer, dwRead);

Which should have the same effect (without seeing the implementation of bufferToString).

Also your comparison is wrong. In c++ you would do

if (cmd == "help")

Comments

-1

This should work:

if (cmd == string("help")) // should work
{
    printf("hello");
}

The code:

if (cmd.c_str() == "help") 

will compare pointers (#1 returned from cmd.c_str() , #2 - constant pointer to "help"), that is not quite correct

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.