read apue

准备所有的类都去掉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);
}

# gcc fig3_11.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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值