Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing strings in C - strcmp
    primarykey
    data
    text
    <p>I'm having trouble comparing strings in C (with which I am fairly new to). I have socket on this server application waiting to accept data from a client. In this particular portion of my program I want to be able to execute a MySQL query based on the data received from the client. I want to be able to know when the received data has the value of "newuser" to initiate a simple registration procedure. Strcmp is returning a positive 1 value where I believe I should be getting a 0 because the values should be equal.</p> <p>Source Code:</p> <pre><code>//setup socket //loop and select structure to handle multiple connections if ((nbytes = recv(i, buf, sizeof buf, 0)) &lt;= 0) { // got error or connection closed by client if (nbytes == 0) { // connection closed printf("selectserver: socket %d hung up\n", i); } else { perror("recv"); } close(i); // bye! FD_CLR(i, &amp;master); // remove from master set } else { char check[] = "newuser"; char fromUser[sizeof check]; strncpy(fromUser,buf, sizeof check); printf("length of fromUser: %d\n", sizeof fromUser); printf("length of check: %d\n", sizeof check); printf("message from user: %s\n", fromUser); printf("check = %s \n", check); int diff = strcmp(fromUser, check); printf("compare fromUser to check: %d\n", diff); if ( strcmp(fromUser, check) == 0) { printf("aha! new user"); } </code></pre> <p>Output:</p> <pre><code>length of fromUser: 8 length of check: 8 newuser from user: newuser check = newuser compare fromUser to check: </code></pre> <p>I have a feeling I'm not handling the incoming buffer correctly or erroneously copying the buffer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload