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 1062065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:34:02+00:00 2026-05-16T18:34:02+00:00

It seems as if read(0, buffer, 255) always returns 1 when reading. In my

  • 0

It seems as if read(0, buffer, 255) always returns 1 when reading. In my program I direct input through the pipe into this program, but I noticed that it always returned a 1. Why is this? Is there a better way of doing this? At the same time, it seems to fill up the buffer properly, past the 1 char.

Thank you in advance!

  • 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-16T18:34:03+00:00Added an answer on May 16, 2026 at 6:34 pm

    read is allowed to return less characters than you asked for, so you should code for that eventuality rather than trying to find a way to avoid it.

    For example, if there’s only 22 bytes left in the file (or the pipe), it will give you those bytes.

    From the Linux man page (since read is a POSIX thing rather than a C++ thing):

    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.

    If the rest of your buffer seems to be populated correctly, I can assure you that it’s entirely by accident and you shouldn’t rely on that. If read returns 1, then only use that one character. If you follow the rules and characters seem to disappear, then you can come back and complain about a buggy read implementation (I shouldn’t need to point out that this is incredibly unlikely).

    Try the following program:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void) {
        char buff[255];
        int i;
    
        while ((i = read (0, buff, 255)) > 0) {
            printf ("%3d: [%*.*s]\n", i, i, i, buff);
        }
    
        return 0;
    }
    

    with:

    echo hello there | ./tstprg ; (echo hello ; sleep 1 ; echo there) | ./tstprg
    

    and see what you get. My output is:

     12: [hello there
    ]
      6: [hello
    ]
      6: [there
    ]
    

    as I expect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.