linux之open函数解析

本文介绍了使用C语言进行文件操作的基本方法,并展示了如何将原始文件备份为后缀为.backup的副本,确保文件的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、 

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fulinux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值