相关疑难解决方法(0)

使用C中的fread从stdin缓冲读取

我试图stdin通过setvbuf在`_IOFBF~模式下使用来有效地读取.我是新来的缓冲.我正在寻找有用的例子.

输入以两个整数(n,k)开头.下一n行输入包含1个整数.目的是打印可以整除的整数数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; …
Run Code Online (Sandbox Code Playgroud)

c buffering fread

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

buffering ×1

c ×1

fread ×1