forked from vonzhou/CSAPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex10-7.c
32 lines (27 loc) · 792 Bytes
/
ex10-7.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* P612, 习题10.7
*/
#include "csapp.h"
// 头文件中定义的MAXBUF = MAXLINE = 8192
#define MYBUF 5
#define MAXLINE 10
int main(int argc, char **argv)
{
int n;
rio_t rio;
char buf[MAXLINE];
int count = 0; //bytes already read
Rio_readinitb(&rio, STDIN_FILENO);
n = Rio_readnb(&rio, buf, MAXLINE);// read a line
while (n > 0) {
count += n;
if(count >= MYBUF) {
Rio_writen(STDOUT_FILENO, buf, MYBUF);
count -= MYBUF;
if (count > 0)
memmove(buf, buf + MYBUF, count);
}
n = Rio_readnb(&rio, buf+count, MAXLINE-count);
}
return 0;
}