验证stdin和stdout为行缓冲

一、源代码:

  1 #include "apue.h"
  2 int main()
  3 {
  4         int c;
  5         while ( (c = getc(stdin)) != EOF)
  6                 if ( putc(c,stdout) == EOF)
  7                         err_sys("putc error");
  8         if (ferror(stdin))
  9                 err_sys("getc error");
 10         exit(0);
 11 }

二、gdb调试:

<bldc:/home/tingbinz/apue.3e/SBSCODE/5>R*_*G:gdb intoout
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "sparc-sun-solaris2.5.1"...
(gdb) l
1       #include "apue.h"
2       int main()
3       {
4               int c;
5               while ( (c = getc(stdin)) != EOF)
6                       if ( putc(c,stdout) == EOF)
7                               err_sys("putc error");
8               if (ferror(stdin))
9                       err_sys("getc error");
10              exit(0);
(gdb) break 4
Breakpoint 1 at 0x10d44: file 5_4.c, line 4.
(gdb) break 5
Note: breakpoint 1 also set at pc 0x10d44.
Breakpoint 2 at 0x10d44: file 5_4.c, line 5.
(gdb) run
Starting program: /home/tingbinz/apue.3e/SBSCODE/5/intoout


Breakpoint 1, main () at 5_4.c:5
5               while ( (c = getc(stdin)) != EOF)
(gdb) n
hello
6                       if ( putc(c,stdout) == EOF)
(gdb) n
6                       if ( putc(c,stdout) == EOF)
(gdb) n
6                       if ( putc(c,stdout) == EOF)
(gdb) n
6                       if ( putc(c,stdout) == EOF)
(gdb) n
6                       if ( putc(c,stdout) == EOF)
(gdb) n
6                       if ( putc(c,stdout) == EOF)
(gdb) n
hello

三、源代码 换为stderr

<bldc:/home/tingbinz/apue.3e/SBSCODE/5>R*_*G:vim 5_4a.c
#include "apue.h"
int main()
{
        int c;
        while ( (c = getc(stdin)) != EOF)
                if ( putc(c,stderr) == EOF)
                        err_sys("putc error");
        if (ferror(stdin))
                err_sys("getc error");
        exit(0);
}

四、编译:

<bldc:/home/tingbinz/apue.3e/SBSCODE/5>R*_*G:gcc -Wall -ggdb3 -o intoerr 5_4a.c
In file included from apue.h:132,
                 from 5_4a.c:1:
error.c: In function `err_doit':
error.c:121: warning: implicit declaration of function `vsnprintf'
error.c:123: warning: implicit declaration of function `snprintf'


五、gdb调试结果:

<bldc:/home/tingbinz/apue.3e/SBSCODE/5>R*_*G:gdb intoerr
GNU gdb 5.0
Copyright 2000 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "sparc-sun-solaris2.5.1"...
(gdb) l
1       #include "apue.h"
2       int main()
3       {
4               int c;
5               while ( (c = getc(stdin)) != EOF)
6                       if ( putc(c,stderr) == EOF)
7                               err_sys("putc error");
8               if (ferror(stdin))
9                       err_sys("getc error");
10              exit(0);
(gdb) break 5
Breakpoint 1 at 0x10d44: file 5_4a.c, line 5.
(gdb) run
Starting program: /home/tingbinz/apue.3e/SBSCODE/5/intoerr


Breakpoint 1, main () at 5_4a.c:5
5               while ( (c = getc(stdin)) != EOF)
(gdb) n
hello
6                       if ( putc(c,stderr) == EOF)
(gdb) n
h6                      if ( putc(c,stderr) == EOF)
(gdb) n
e6                      if ( putc(c,stderr) == EOF)
(gdb) n
l6                      if ( putc(c,stderr) == EOF)
(gdb) n
l6                      if ( putc(c,stderr) == EOF)
(gdb) n
o6                      if ( putc(c,stderr) == EOF)
(gdb) n


world
6                       if ( putc(c,stderr) == EOF)
(gdb) n
w6                      if ( putc(c,stderr) == EOF)
(gdb)




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值