首页 > 代码库 > 【linux程序设计4th】第三章1

【linux程序设计4th】第三章1

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);
    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>

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);
    }
    return 0;
}

/*======================================+
                                        +
[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;
    int in, 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);
    
    return 0;
}

主要几点需要记住

1.  unistd.h头文件必须放在前面,它定义了posix有关的标志会影响到其他头文件。

2.  对文件,管道命令的理解

3. sizeof()和strlen() 对字符串求长度有点区别

4. read() write() 对空格 制表符 回车换行的处理

2016年11月23日22:54:51

【linux程序设计4th】第三章1