您的位置:首页 > 运维架构 > Linux

linux之open函数解析

2013-07-28 13:01 246 查看
一、 

open.c                                                                                                         

/*********************************************************************************

 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 

 *                  All rights reserved.

 *

 *       Filename:  open.c

 *    Description:  This file 

 *                 

 *        Version:  1.0.0(07/27/2013~)

 *         Author:  fulinux <fulinux@sina.com>

 *      ChangeLog:  1, Release initial version on "07/27/2013 03:23:14 PM"

 *                 

 ********************************************************************************/

#include <stdio.h>

#include <string.h>

#include <fcntl.h>

int main(void)

{

    int fd;

    if(fd = open("openfile", O_CREAT | O_RDWR, S_IRUSR|S_IWUSR|S_IXUSR))

        printf("open is OK\n");

    else

        printf("open is not OK\n");

    if(!close(fd))

        printf("closed\n");

    else

        printf("not closed\n");
}

[lingyun@localhost apue]$ gcc open.c 

[lingyun@localhost apue]$ ls

a.out  open.c

[lingyun@localhost apue]$ ./a.out 

open is OK

closed

[lingyun@localhost apue]$ ls

a.out  open.c  openfile

[lingyun@localhost apue]$ 

二、

[lingyun@localhost open_2]$ vim open.c

 ********************************************************************************/

        perror("open");

        printf("%s",str);

 + open.c                                                                                                       

/*********************************************************************************

 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 

 *                  All rights reserved.

 *

 *       Filename:  open.c

 *    Description:  This file 

 *                 

 *        Version:  1.0.0(07/28/2013~)

 *         Author:  fulinux <fulinux@sina.com>

 *      ChangeLog:  1, Release initial version on "07/28/2013 12:04:54 PM"

 *                 

 ********************************************************************************/

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <fcntl.h>

int main(int argc,char *argv[])

{

    int fd;

    char str[10];

    fd = open("hello.txt",O_RDONLY);

    if(fd < 0)

    {

        perror("open");

    }

    while(read(fd,str,sizeof(str)) > 0)

    {

        printf("%s",str);

    }

    return 0;

}

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

~                                                                                                               

 ~/apue/open_2/open.c[+]   CWD: /usr/local/src/lingyun/apue/open_2   Line: 22/38:15                             

"open.c" [New] 38L, 875C written

[lingyun@localhost open_2]$ vim hello.txt

 + hello.txt                                                                                                    

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

ok

~                                                                                                                                                                                                                       

~                                                                                                               

 ~/apue/open_2/hello.txt[+]   CWD: /usr/local/src/lingyun/apue/open_2   Line: 28/28:2                           

"hello.txt" [New] 28L, 543C written

[lingyun@localhost open_2]$ ls

hello.txt  open.c

[lingyun@localhost open_2]$ gcc open.c 

[lingyun@localhost open_2]$ ./a.out 

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

hello world !!!!!!!

ok

!!!!!!

[lingyun@localhost open_2]$ 

三、

  1 /*********************************************************************************

  2  *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 

  3  *                  All rights reserved.

  4  *

  5  *       Filename:  open.c

  6  *    Description:  This file 

  7  *                 

  8  *        Version:  1.0.0(07/28/2013~)

  9  *         Author:  fulinux <fulinux@sina.com>

 10  *      ChangeLog:  1, Release initial version on "07/28/2013 12:14:31 PM"

 11  *                                                                                                                 

 12  ********************************************************************************/

 13 

 14 #include <stdio.h>

 15 #include <stdlib.h>

 16 #include <string.h>

 17 #include <fcntl.h>

 18 

 19 int main(int argc,char *args[])

 20 {

 21 

 22     char buff[1024];

 23     int fd1,fd2,i;

 24     int baksize = sizeof(args[1]) + 7;

 25     char bakfile[baksize];

 26 

 27     /* input one file only */

 28     if(argc != 2)

 29     {

 30         printf("Input one file a time!\n");

 31         exit(1);

 32     }

 33 

 34     /* bakfile="XXX.backup" */

 35     strcpy(bakfile,args[1]);

 36     strcat(bakfile,".backup");

 37 

 38     /* open() */

 39     fd1 = open(args[1], O_RDONLY, 0644);

 40     fd2 = open(bakfile, O_RDWR|O_CREAT|O_TRUNC);

 41 

 42     if(fd1 < 0||(fd2 < 0))

 43     {

 44         printf("Open Error! Check if the file is exist and you have permission!\n");

 45         exit(1);

 46     }

 47 

 48     /* read from fd1 and write buff to fd2 */

 49     while((i = read(fd1,buff,sizeof(buff))) > 0)

 50     {

 51         write(fd2,buff,i);

 52     }

 53 

 54     close(fd1);

 55     close(fd2);

 56     printf("Backup done!\n");

 57     exit(0);

 58 

 59 }

[lingyun@localhost open_3]$ vim file.txt

hello world !!!!                                                                                                    

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

end

[lingyun@localhost open_3]$ ls

a.out  file.txt  open.c

[lingyun@localhost open_3]$ ./a.out file.txt 

Backup done!

a.out  file.txt  file.txt.backup  open.c

[lingyun@localhost open_3]$ cat file.txt.backup 

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

hello world !!!!

end

[lingyun@localhost open_3]$ 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: