您的位置:首页 > 其它

验证stdin和stdout为行缓冲

2015-09-23 14:09 323 查看
一、源代码:

  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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: