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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:51:29+00:00 2026-05-26T21:51:29+00:00

I have a requirement of waiting using read until a buffer is full on

  • 0

I have a requirement of waiting using “read” until a buffer is full on an audio codec device. For make it easier, lets take similar example of:

 fd= read(fileno(stdin), &buf, 10);

How can I return from the read when I type 10 characters in stdin? (I hope if this is success, I can wait on codec until specified bytes of data is arrived).

The above example needs an “Enter Key” from console, where as I want “read” to unblock only when desired bytes of data is arrived.

EDIT: Requirement is waiting using a single “read” till specified bytes are arrived.

  • 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-26T21:51:29+00:00Added an answer on May 26, 2026 at 9:51 pm

    How about this:

    #define BUFFER_SIZE 10
    char buffer[BUFFER_SIZE];
    
    /* ... */
    
    size_t total_read = 0;
    size_t total_left = BUFFER_SIZE;  /* The total size of the buffer */
    char *buffer_pointer = buffer;    /* buffer is where to store the data */
    while (total_left > 0)
    {
        ssize_t current = read(STDIN_FILENO, buffer_pointer, total_left);
        if (current <= 0)
        {
            /* Error or end of file */
            if (current < 0)
                perror("read");  /* Error! */
            break;
        }
        else
        {
            total_read += current;     /* We have read some more data */
            total_left -= current;     /* Less data left to read */
            buffer_pointer += current; /* So we don't read over already read data */
        }
    }
    
    printf("Received %ld characters\n", total_read);
    for (unsigned int i = 0; i < total_read; i++)
        printf("Character #%d: '%c'\n", i, buffer[i]);
    

    Beware that this will block your program until all data is read. The amount of characters read may be less than everything, because there might be an error or the user pressed CTRL-D (end of file).

    Also note that the STDIN_FILE file-descriptor is most likely connected to a tty, which means it might be buffered, so doesn’t return data until newline, and that you might have to make the tty unbuffered.

    Edit
    To make sure the tty connected to stdin is unbuffered, use the following code:

    #include <termios.h>
    
    /* ... */
    
    /* Somewhere before reading from stdin... */
    struct termios tty_settings;
    
    tcgetattr(STDIN_FILENO, &tty_settings);
    tty_settings.c_lflag &= ~ICANON;
    tcsetattr(STDIN_FILENO, TCSANOW, &tty_settings);
    

    For more information about the tcsetattr function and the ICANON flag, check the manual page for tcsetattr.

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

Sidebar

Related Questions

I have a requirement to make a large amount of code MISRA compliant. First
We have a requirement to increase the functionality of a grid we are using
We have the requirement to take a form submission and save some data, then
I have a requirement to take the test data from Excel sheet and write
I have a circular, statically allocated buffer in C, which I'm using as a
I have a request from users to modify Microsoft CRM to make it's read-only
I'm using NServiceBus to handle some calculation messages. I have a new requirement to
i am working in a web application using java language.i have a requirement to
I have requirement of specifying web part connections in onet.xml. So when site is
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field

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.