`
haoningabc
  • 浏览: 1444652 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

read apue

    博客分类:
  • c
阅读更多
准备所有的类都去掉apue.h就niuB了,fig1.4开始
ok
用vim解决,ctags -R -f systag /usr/include/ /mydir
把systag加入~/.vimrc
vim编辑的时候control+]跳到找不到了定义就行了

在P O S I X . 1应用程序中,幻数0、1、2应被代换成符号常数S T D I N F I L E N O、S T D O U T F I L E N O和S T D E R R F I L E N O。这些常数都定义在头文件< u n i s t d . h >中。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define BUFFSIZE    4096

int
main(void)
{
    int     n;
    char    buf[BUFFSIZE];

    while ((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
        if (write(STDOUT_FILENO, buf, n) != n)
            printf("write error");

    if (n < 0)
        printf("read error");

    exit(0);
}


fcntl.h与unistd.h
http://baike.baidu.com/view/3522799.htm

----------seek-------
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int
main(void)
{
    if (lseek(STDIN_FILENO, 0, SEEK_CUR) == -1)
        printf("cannot seek\n");
    else
        printf("seek OK\n");
    exit(0);
}

[root@red54apple c]# gcc fig3_11.c
[root@red54apple c]# ./a.out < epoll-test.c
,不要测试它是否小于0,而要测试它是否等于-1。
--------------getopt----
函数说明 getopt()用来分析命令行参数。参数argc和argv是由main()传递的参数个数和内容。参数 optstring为选项字符串, 告知 getopt()可以处理哪个选项以及哪个选项需要参数,如果选项字符串里的字母后接着冒号“:”,则表示还有相关的参数,全域变量optarg 即会指向此额外参数。如果在处理期间遇到了不符合optstring指定的其他选项getopt()将显示一个错误消息,并将全域变量optopt设为“?”字符,如果不希望getopt()印出错信息,则只要将全域变量opterr设为0即可
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
        int ch;
        opterr = 0;
        while((ch = getopt(argc,argv,"a:bcde"))!= -1)
                switch(ch)
                {
                        case 'a':
                                printf("option a:’%s’\n",optarg);
                                break;
                        case 'b':
                                printf("option b :b\n");
                                break;
                        default:
                                printf("other option :%c\n",ch);
                }
        printf("optopt +%c\n",optopt);
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics