Makefile
. Phony:clean Allcc=gcccflags=-wall-g # # #replace your binbin=simple_write simple_read copy_system all:$ (BIN)%.o:%.c$ (CC ) $ (CFLAGS)-C $< -o [email protected]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 header file must be placed in front, it defines POSIX related flags that affect other header files*/#include<unistd.h>#include<string.h>intMain () {Char*str="Test the Write function for the first time \ n"; intn=strlen (str); if(Write (1, str,n)! =N) Write (2,"error\n",6); return 0;}
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>intMain () {Charbuf[1024x768]; intnread; Nread=read (0, BUF,1024x768); 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); } return 0;}/*======================================+ +[[email protected] 3rd]$./simple_read +hello World +hello World +[[email Protected] 3rd]$./simple _read +hello World +hello World +[[email Protected] 3rd]$ + + Note space, tab, enter + pipe command echo "Hello" |./simple_r EAD +========================================*/
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 <un Istd.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>intMain () {CharC; int inch, out; inch= Open ("file.in", o_rdonly);/*file.in prepare himself * * out=open ("File.out", o_wronly| O_creat, s_irusr|s_iwusr); while(Read (inch, &c,1)==1) Write ( out, &c,1); return 0;}
The main points to remember
1. The unistd.h header file must be placed in front of it, which defines POSIX-related flags that affect other header files.
2. Understanding of documents, piping commands
3. sizeof () and strlen () have a somewhat different string length.
4. Read () write () Handling of the Space tab carriage return line
November 23, 2016 22:54:51
"Linux Programming 4th" Chapter 1