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

  • SEARCH
  • Home
  • 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 6951279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:11:56+00:00 2026-05-27T14:11:56+00:00

all, I am designing a Key-Value server, and when I wrote the client, and

  • 0

all, I am designing a Key-Value server, and when I wrote the client, and I found a really strange thing,see the simplified code:

while(1)
{
    printf("->:");
    read(STDIN_FILENO, buf, sizeof(buf));
    write(client_sock, buf, sizeof(buf));
    int m = read(client_sock, buf, sizeof(buf));
    buf[m] = '\0';
    printf("%s", buf);
}

when I run the program, it first ask for input, so I input something, but nothing happen!
(the server runs well, and it well echo something, when I use other client)

then I change the code only one line:

printf("\n->:");

then it runs well! why? why “\n” can change the output? I guess it maybe the read() , but I can’t explain it

  • 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-05-27T14:11:57+00:00Added an answer on May 27, 2026 at 2:11 pm

    printf(3) is part of the C standard IO library, which performs internal buffering to provide performance improvements.

    There are three types of buffering: none, line, and block.

    Which buffering is applied is determined in part by whether the descriptor being written to is 2 or not, and if it is connected to a terminal. (See isatty(3).)

    If the printing is done to stderr (2) then no buffering is done.

    If the printing is done to any other descriptor, then the behavior changes if it is a terminal or not: if output is a terminal, then the output is line buffered. If the output is not a terminal (file, pipe, socket, etc.) then the output is block buffered.

    When line buffered, it waits for the \n before printing anything. (Or if you write enough to overflow the internal buffers before sending a \n.)

    What I’d recommend instead is the following:

    printf("->:");
    fflush(stdout);
    read(STDIN_FILENO, buf, sizeof(buf));
    /* ... */
    printf("%s\n", buf);
    

    It’s a small change; you won’t get a pointless empty line at program start, and the prompt should show up .. promptly.

    You can use the setvbuf(3) function to change the buffering for your stream once, at start up, and never need to flush it again, if you would rather.

    int err = setvbuf(stdout, NULL, _IONBF, 0);
    /* check err */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a distinct winner among all the key-value stores? Cassandra, MongoDB, CouchDB? and
With all the key value data stores out there I have started to create
Hi All, I'm designing a user control, briefly it contains an asp:hiddenfield control, i'm
I have a userlist for a program I'm designing, and all the users are
All the articles I've found via google are either obsolete or contradict one another.
All the recent VisualSVN Server posts made me want to check it out. I
All - I need some help designing a table for a Postgre SQL database.
I want to know the key points, which one should keep in mind while
I've recently been reading Louis Davidson's book on Sql Server Database Design and found
I am designing my database using code first and I need a little help

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.