zoukankan      html  css  js  c++  java
  • APUE_1.5InputAndOutput Figure1.5CopyStandardInputToStandardOutputUsingStandardIO

    XFL)V%_}KN{CI4~9OSEGTMT

    G71YEBQSZB4K(D3MF[7WBCO

    /*
     * 1.5CopyStandardInputToStandardOutputUsingStandardIO.cpp
     *
     *  Created on: Feb 10, 2015
     *      Author: sunyj
     */
    #include "../apuesunyj.h"
    
    /*
    The standard I/O functions provide a buffered interface to the unbuffered I/O
    functions. Using standard I/O relieves us from having to choose optimal buffer sizes,
    such as the BUFFSIZE constant in Figure 1.4. The standard I/O functions also simplify
    dealing with lines of input (a common occurrence in UNIX applications). The fgets
    function, for example, reads an entire line. The read function, in contrast, reads a
    specified number of bytes. As we shall see in Section 5.4, the standard I/O library
    provides functions that let us control the style of buffering used by the library.
    The most common standard I/O function is printf. In programs that call
    printf, we will always include <stdio.h> normally by including apue.h as this
    header contains the function prototypes for all the standard I/O functions.
    
    The function getc reads one character at a time, and this character is written by putc.
    After the last byte of input has been read, getc returns the constant EOF (defined in
    <stdio.h>). The standard I/O constants stdin and stdout are also defined in the
    <stdio.h> header and refer to the standard input and standard output.
     */
    int main()
    {
    	int c;
    
    	while ((c = getc(stdin)) != EOF)
    	{
    		if (putc(c, stdout) == EOF)
    		{
    			err_sys("output error");
    		}
    	}
    
    	if (ferror(stdin))
    	{
    		err_sys("input error");
    	}
    
    	return 0;
    }
    
     1 // apuesunyj.h
     2 #ifndef APUE_SUNYJ
     3 #define APUE_SUNYJ
     4 
     5 #include <errno.h>
     6 #include <stdio.h>
     7 #include <string.h>
     8 #include <stdarg.h>
     9 #include <stdlib.h>
    10 #include <stdint.h>
    11 #include <unistd.h>
    12 
    13 void err_quit(const char *fmt, ...);
    14 void err_sys(const char *fmt, ...);
    15 
    16 
    17 #endif
     1 // error.cpp
     2 #include "apuesunyj.h"
     3 
     4 int64_t const MAXLINE = 4096; // max line length
     5 /*
     6  *  * Print a message and return to caller.
     7  *   * Caller specifies "errnoflag".
     8  *    */
     9 static void err_doit(int errnoflag, int error, const char *fmt, va_list ap)
    10 {
    11     char buf[MAXLINE];
    12 
    13     vsnprintf(buf, MAXLINE, fmt, ap);
    14     if (errnoflag)
    15         snprintf(buf + strlen(buf), MAXLINE - strlen(buf), ": %s", strerror(
    16                     error));
    17     strcat(buf, "
    ");
    18     fflush(stdout);/* in case stdout and stderr are the same */
    19     fputs(buf, stderr);
    20     fflush(NULL);/* flushes all stdio output streams */
    21 }
    22 
    23 /*
    24  *  * Fatal error unrelated to a system call.
    25  *   * Print a message and terminate.
    26  *    */
    27 void err_quit(const char *fmt, ...)
    28 {
    29     va_list ap;
    30 
    31     va_start(ap, fmt);
    32     err_doit(0, 0, fmt, ap);
    33     va_end(ap);
    34     exit(1) ; // process terminate, not just like return, totally different
    35 }
    36 
    37 /*
    38  *  * Fatal error related to a system call.
    39  *   * Print a message and terminate.
    40  *    */
    41 void err_sys(const char *fmt, ...)
    42 {
    43     va_list ap;
    44 
    45     va_start(ap, fmt);
    46     err_doit(1, errno, fmt, ap);
    47     va_end(ap);
    48     exit(1) ;
    49 }
    # libApueSunyj.a
    libApueSunyj.a: error.o
        ar cr libApueSunyj.a error.o
    error.o: error.cpp apuesunyj.h
        g++ -c -g error.cpp -o error.o
    clean:
        rm error.o libApueSunyj.a

    IMG_1272IMG_1273IMG_1274

    IMG_1275

    IMG_1276IMG_1277IMG_1278IMG_1279

    IMG_1280IMG_1281

  • 相关阅读:
    跟vczh看实例学编译原理——二:实现Tinymoe的词法分析
    跟vczh看实例学编译原理——一:Tinymoe的设计哲学
    跟vczh看实例学编译原理——零:序言
    2013年终总结
    如何设计一门语言(十二)——设计可扩展的类型
    开始用Word 2013来写博客
    如何设计一门语言(十一)——删减语言的功能
    如何设计一门语言(十)——正则表达式与领域特定语言(DSL)
    链表
    结构的学习
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4284906.html
Copyright © 2011-2022 走看看