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]$
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]$
相关文章推荐
- Linux系统文件I/O编程(一)---open()等基本函数
- 解析Linux下的时间函数:设置以及获取时间的方法
- linux之dup和dup2函数解析
- linux之unlink函数解析
- Linux系统中C语言编程创建函数fork()执行解析
- 【Linux】dup和dup2函数解析与文件描述符
- Linux 多线程函数解析
- Linux中C语言open函数打开或创建文件详细讲解
- Linux atexit函数解析
- linux之umask函数解析
- Linux 字符设备驱动开发基础(四)—— ioctl() 函数解析
- linux_011之库函数execve.c解析
- Intel平台下linux中 ELF文件动态链接的加载、解析及实例分析(二): 函数解析与卸载
- Linux系统函数open和close(03)---open函数的参数
- Linux文件(区域)锁函数 -- open()、fcntl()
- Linux 多线程函数解析
- LINUX下OPEN函数简介
- 上传 qnx 6.32专业版帮助 unix linux命令及c c++函数解析 类linux的msdn
- linux之stat函数解析
- linux之stat函数解析