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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:04:48+00:00 2026-05-13T23:04:48+00:00

I am trying to efficiently read from the stdin by using setvbuf in `_IOFBF~

  • 0

I am trying to efficiently read from the stdin by using setvbuf in `_IOFBF~ mode. I am new to buffering. I am looking for working examples.

The input begins with two integers (n,k). The next n lines of input contain 1 integer. The aim is to print how many integers are divisible by k.

#define BUFSIZE 32
int main(){
  int n, k, tmp, ans=0, i, j;
  char buf[BUFSIZE+1] = {'0'};
  setvbuf(stdin, (char*)NULL, _IONBF, 0);
  scanf("%d%d\n", &n, &k);
  while(n>0 && fread(buf, (size_t)1, (size_t)BUFSIZE, stdin)){
    i=0; j=0;
    while(n>0 && sscanf(buf+j, "%d%n", &tmp, &i)){
    //printf("tmp %d - scan %d\n",tmp,i); //for debugging
      if(tmp%k==0)  ++ans;
      j += i; //increment the position where sscanf should read from
      --n;
    }
  }
  printf("%d", ans);
  return 0;
}

The problem is if number is at the boundary, the buffer buf will read 23 from 2354\n, when it should have either read 2354 (which it cannot) or nothing at all.

How can I solve this issue?


Edit
Resolved now (with analysis).

Edit
Complete Problem Specification

  • 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-13T23:04:49+00:00Added an answer on May 13, 2026 at 11:04 pm

    Version 1 : Using getchar_unlocked as suggested by R Samuel Klatchko (see comments)

    #define BUFSIZE 32*1024
    int main(){
      int lines, number=0, dividend, ans=0;
      char c;
      setvbuf(stdin, (char*)NULL, _IOFBF, 0);// full buffering mode
      scanf("%d%d\n", &lines, &dividend);
      while(lines>0){
        c = getchar_unlocked();
        //parse the number using characters
        //each number is on a separate line
        if(c=='\n'){
          if(number % dividend == 0)    ans += 1;
          lines -= 1;
          number = 0;
        }
        else
          number = c - '0' + 10*number;
      }
    
      printf("%d are divisible by %d \n", ans, dividend);
      return 0;
    }

    Version 2: Using fread to read a block and parsing number from it.

    #define BUFSIZE 32*1024
    int main(){
    int lines, number=0, dividend, ans=0, i, chars_read;
    char buf[BUFSIZE+1] = {0}; //initialise all elements to 0
    scanf("%d%d\n",&lines, &dividend);
    
    while((chars_read = fread(buf, 1, BUFSIZE, stdin)) > 0){
      //read the chars from buf
      for(i=0; i < chars_read; i++){
        //parse the number using characters
        //each number is on a separate line
        if(buf[i] != '\n')
          number = buf[i] - '0' + 10*number;
        else{
          if(number%dividend==0)    ans += 1;
          lines -= 1;
          number = 0;
        }       
      }
    
    if(lines==0)  break;
    }
    
    printf("%d are divisible by %d \n", ans, dividend);
    return 0;
    }
    

    Results: (10 million numbers tested for divisibility by 11)

    Run 1: ( Version 1 without setvbuf ) 0.782 secs
    Run 2: ( Version 1 with setvbuf ) 0.684 secs
    Run 3: ( Version 2 ) 0.534

    P.S. – Every run compiled with GCC using -O1 flag

    • 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.