【linux程序设计4th】第三章1
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【linux程序设计4th】第三章1,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2175字,纯文字阅读大概需要4分钟。
内容图文
makefile
.PHONY:clean all CC=gcc CFLAGS=-Wall -g ###replace your bin BIN=simple_write simple_read copy_system all:$(BIN) %.o:%.c $(CC) $(CFLAGS) -c $< -o $@ clean: rm -f *.o $(BIN)
simple_write.c
/* #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); #include <string.h> size_t strlen(const char *s); */ /* unistd.h头文件必须放在前面, 它定义了posix有关的标志会影响到其他头文件 */ #include <unistd.h> #include <string.h> int main() { char *str="第一次测试write函数\n"; int n=strlen(str); if(write(1,str,n) != n) write(2,"error\n",6); return0; }
simple_read.c
/* //man 3 exit #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); #include <stdlib.h> void exit(int status); */ #include <stdlib.h> #include <unistd.h> int main() { char buf[1024]; int nread; nread=read(0, buf, 1024); if(nread == -1){ write(2,"error\n",sizeof("error\n")); exit(-1); } if(write(1,buf,nread) != nread){ write(2,"error\n",sizeof("error\n")); exit(-1); } return0; } /*======================================+ + [shuai@shuaiPC 3rd]$ ./simple_read + hello world + hello world + [shuai@shuaiPC 3rd]$ ./simple_read + hello world + hello world + [shuai@shuaiPC 3rd]$ + + 注意 空格,制表,回车 + 管道命令 echo "hello" | ./simple_read + ========================================*/
copy_system.c
/* #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); #include <unistd.h> int close(int fd); #include <sys/ioctl.h> int ioctl(int d, int request, ...); */ #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { char c; intin, out; in = open("file.in", O_RDONLY);/*file.in自己准备*/ out=open("file.out", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); while(read(in, &c, 1)==1) write(out,&c, 1); return0; }
主要几点需要记住
1. unistd.h头文件必须放在前面,它定义了posix有关的标志会影响到其他头文件。
2. 对文件,管道命令的理解
3. sizeof()和strlen() 对字符串求长度有点区别
4. read() write() 对空格 制表符 回车换行的处理
2016年11月23日22:54:51
原文:http://www.cnblogs.com/startnow/p/6095898.html
内容总结
以上是互联网集市为您收集整理的【linux程序设计4th】第三章1全部内容,希望文章能够帮你解决【linux程序设计4th】第三章1所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。