Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:23:20+00:00 2026-06-18T09:23:20+00:00

I came across a weird problem in c programming today. I wrote a program

  • 0

I came across a weird problem in c programming today. I wrote a program for server client communication and applied the string comparison function on it but the function doesn’t work if I compare a string with stuff inside buf. I also checked if data in buf is different than what I am entering after a few iteration but results were negative and the data is the same as I am entering. Then why does strcmp is not working. Here is the code:

char buf[1024];
while(1)
{
    int readbytes=read(communFd,buf,1024);

    write(STDOUT_FILENO,buf,readbytes);


   if(!strcmp(buf,"exitChat"))
    {
        printf("Chat terminating...\n");
        break;
    }
}

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:23:21+00:00Added an answer on June 18, 2026 at 9:23 am

    read() does not null terminate the buffer which strcmp() depends upon, you must do this explicitly:

    int readbytes=read(communFd,buf,1023); /* Read one less for null terminator. */
    if (readBytes != -1)
    {
        buf[readBytes] = 0;
        if(!strcmp(buf,"exitChat"))
        {
        }
    }
    

    Or, as mentioned by others use a different function that does not depend upon the presence of the null terminator character.

    Note that read() does not guarantee that the requested number of bytes is read and it is not considered an error if less bytes is read. From the linked reference page:

    On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.

    To ensure that a full message has been received you will need to add data to text being sent so the receiver knows when it has received a complete message. This could be:

    • preceding the text with its length.
    • using a sentinel value to indicate the end of a message, for example a \n character to indicate the end. The structure of the code would change to call read() in a loop until the \n is found while appending each byte read to an array.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across something really weird when I wrote a little lotto program in
I just came across this weird problem that is happening in IE. In the
I came across something weird today, and I was wondering if any of you
I came across this weird C++ program. #include <iostream> using namespace std; int main()
I came across a weird little problem. After searching around to fix the refreshing
This is kinda weird problem we came across with my friend. We located our
I came across a weird situation today while coding and I'm hoping someone could
Possible Duplicate: Strange floating-point behaviour in a Java program I came across this weird
Actually I am devloping a php application and I came across a weird problem.
I have to debug a weird PHP application and today I came across a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.