linux open函数解释,linux之open函数解析

一、

open.c /*********************************************************************************

*      Copyright:  (C) 2013 fulinux

*                  All rights reserved.

*

*       Filename:  open.c

*    Description:  This file

*

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

*         Author:  fulinux

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

*

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

#include

#include

#include

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

*                  All rights reserved.

*

*       Filename:  open.c

*    Description:  This file

*

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

*         Author:  fulinux

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

*

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

#include

#include

#include

#include

#include

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

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

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

11  *

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

13

14 #include

15 #include

16 #include

17 #include

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]$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值