UNIX环境高级编程程序1-2将标准输入复制到标准输出
2015-01-28 21:17
423 查看
__
#defineBUFFSIZE4096
intmain()
{
intn;
charbuf[BUFFSIZE];
//Byconvention,allshellsopenthreedescriptorswheneveranewprogramisrun:
//standardinput,standardoutput,andstandarderror.
//STDIN_FILENOspecifythefiledescriptorforstandardinput
//UnbufferedI/Oisprovidedbythefunctionsopen,read,write,lseek,andclose.
//Thesefunctionsallworkwithfiledescriptors.
//Thereadfunctionreturnsthenumberofbytesthatareread,andthisvalueisused
//asthenumberofbytestowrite.Whentheendoftheinputfileisencountered,read
//returns0andtheprogramstops.Ifareaderroroccurs,readreturns-1.
while((n=read(STDIN_FILENO,buf,BUFFSIZE))>0)
{
if(write(STDOUT_FILENO,buf,n)!=n)
{
err_sys("writeerror");
}
}
//usuallynisbiggerthan0,ifinputctrl+d
//thennisequalto0
if(n<0)
{
err_sys("readerror");
}
return0;
}
//ThestandardI/OfunctionsprovideabufferedinterfacetotheunbufferedI/O
//functions.UsingstandardI/Orelievesusfromhavingtochooseoptimalbuffersizes,
//suchastheBUFFSIZEconstantinFigure1.4.ThestandardI/Ofunctionsalsosimplify
//dealingwithlinesofinput(acommonoccurrenceinUNIXapplications).Thefgets
//function,forexample,readsanentireline.Thereadfunction,incontrast,readsa
//specifiednumberofbytes.AsweshallseeinSection5.4,thestandardI/Olibrary
//providesfunctionsthatletuscontrolthestyleofbufferingusedbythelibrary.
//ThemostcommonstandardI/Ofunctionisprintf.Inprogramsthatcall
//printf,we隆炉llalwaysinclude<stdio.h>normallybyincludingapue.hasthis
//headercontainsthefunctionprototypesforallthestandardI/Ofunctions.
makefile
g++-g-Wallmycat.c../lib/libapue.a-I../include-omycat
clean:
rmmycat
相关文章推荐
- UNIX环境高级编程1-3用标准I/O将标准输入复制到标准输出
- UNIX环境高级编程 chat1 从标准输入中读取命令执行
- unix环境高级编程-输入和输出
- APUE(UNIX环境高级编程)程序架构编译分析(Makefile)
- UNIX环境高级编程——标准IO-实现查看所有用户
- UNIX环境高级编程-第5章- 标准 I/O 库
- (转载)unix环境高级编程 -- APUE中的第一个程序-myls如何才能运行
- 解决unix环境高级编程的第一个程序运行问题
- APUE(UNIX环境高级编程)程序架构编译分析(Makefile)
- UNIX环境高级编程中的11章程序11-2编译问题---undefined reference to `pthread_create'
- UNIX环境高级编程5.9二进制IO 输入输出流理解
- Unix学习笔记------书本例题编程实例------从标注输入读入然后从标准输出输出
- 解决unix环境高级编程的第一个程序运行问题
- unix 环境高级编程 chap5 - 标准I/O
- UNIX环境高级编程1-5从标准输入读命令并执行
- unix环境高级编程-5.8-标准I/O的效率。
- APUE(UNIX环境高级编程)程序架构编译分析(Makefile)
- UNIX环境高级编程——标准IO-实现查看所有用户
- UNIX环境高级编程学习之第五章标准IO-实现查看所有用户
- 程序3-3 将标准输入复制到标准输出