|
![]() | 作者: cimsxiyang [cimsxiyang]
![]() |
登录 |
文件i/o 先说说几个基本概念: 文件:unix对文件的格式是没有限制的,每个文件对于os来说都是无格式的字节流。一直遇到文件结束符。 系统调用:内核中设置了一组用于实现各种系统功能的子程序,称为系统调用。 函数调用:调用函数。:) 二者很相似,却又有区别;区别: 系统调用由操作系统核心提供,运行于核心态;而普通的函数调用由函数库或用户自己提供,运行于用户态。 系统调用的函数有哪些: 基本的如下:open(),create(),write(),read(),lseek(),close()等等。 文件描述符: 文件描述符是一个和打开的文件相关联的非负整数。 在很多的情况下,我们需要将一个打开的文件描述符从一个进程传递给另一个进程。在UNIX下,最常见的方法是在进程链上进行的由父进程向子进程的文件描述符的传递 父进程通过creat()、open()、pipe()、dup()等系统调用得到fd,----父进程调用fork()生成子进程。这时文件描述符就已经自动从父进程传递给子进程了。 ------因为父进程和子进程往往要进行不同的工作,所以通常子进程还要通过调用exec()来执行真正的工作用程序。 /*当然也有子进程想父进程传递的例程*/ 大家可以看出系统调用是多么的重要。 函数原型: ・ #include <sys/types.h> ・ #include <sys/stat.h> ・ #include <fcntl.h> ・ int open(const char pathname, int flags,/ *mode_t mode*/); ・ 文件的打开标志: flags:O_RDONLY(只读),O_WRONLY(可写),O_RDWR(可读写)(很显然这三个只能用一个) O_CREA ・ O_NOCTTY ・ O_TRUNC ・ O_APPEND ・ O_NONBLOCK/O_NDELAY ・ O_SYNC ・ O_EXCL ・ 文件的访问许可: mode ・ S_IRWXU 00700 ・ S_IRUSR (S_IREAD) 00400 ・ S_IWUSR (S_IWRITE) 00200 ・ S_IXUSR (S_IEXEC) 00100 ・ ・ S_IRWXG 00070 ・ S_IRGRP 00040 ・ S_IWGRP 00020 ・ S_IXGRP 00010 ・ ・ S_IRWXO 00007 ・ S_IROTH 00004 ・ S_IWOTH 00002 ・ S_IXOTH 00001 注意点: 指定 O_CREATE 时, 必须指定 mode 创建新文件有二种方法creat(), open (pathname, O_CREAT | O_WRONLY | O_TRUNC, mode) create()/create a new file*/ ・ #include <sys/types.h> ・ #include <sys/stat.h> ・ #include <fcntl.h> ・ int creat(const char *pathname, mode_t mode); notes:creat()只能以O_WRONLY打开创建文件 creat()不能创建设备文件 read()/write()/*read()---read data from file,write()将memory中的数据写入已经打开的文件*/ ・ #include <unistd.h> ・ ・ ssize_t read(int fd, void *buf, size_t count); ・ ssize_t write(int fd, const void *buf, size_t count); lseek()/*定位一个已经打开的文件*/ ・ #include <sys/types.h> ・ #include <unistd.h> ・ off_t lseek(int fildes, off_t offset, int whence); close()/*close fd*/ #include <unistd>. Int close (int filedes); Dup(),dup2()/*cp一个fd*/ #include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd); fcntl()/*对fd进行各种操作*/ #include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd); int fcntl(int fd, int cmd, long arg); int fcntl(int fd, int cmd, struct flock * lock); ioctl()/*控制设备*/ #include <sys/ioctl.h> int ioctl(int d, int request, ...) 好了,现在我写一些例程来说明问题。 /*open.c*/ #include <sys/types.h> ・ #include <sys/stat.h> ・ #include <error.h> ・ #include <fcntl.h> ・ #include <stdio.h> ・ extern int errno ・ main() ・ { ・ int fd; ・ if ((fd=open(“xiyang.c”,O_CREAT|O_EXCL,S_IRWXU))<0) ・ { ・ printf(“open error! \n error NO is %d”,errno); ・ if (errno==EEXIT) ・ printf(“the file already exist”); ・ } ・ else printf(“sucessful!”); ・ return 0; ・ } gcc �Co open open.c ./open 我们在当前目录下看一下,是否有xiyang.c,if 没有,then sucessful. else print error 当然,我们可以手工加一个xiyang.c看一下也无妨. /*Creat.c*/ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <error.h> extern errno main() { int fd; if((fd=creat(“xiyang.c”,S_IWRITE))<0) { printf(“create error, \n,error no is %d”errno); if (errno==EROFS) printf(“sorry,pession defined!”); } else printf(“sucessful!”); } 我们可以这样: chmod u-w xiyang.c gcc一下 我们可以看到sorry permission denied. ・ #include <sys/types.h> ・ #include <sys/stat.h> ・ #include <unistd.h> ・ #include <fcntl.h> ・ #include <stdio.h> ・ ・ char buf1 [] = "abcdefghij"; ・ char buf2 [] = "ABCDEFGHIJ"; ・ ・ void err_sys (const char* info) ・ { ・ perror (info); ・ exit (1); ・ } ・ ・ int main (void) ・ { ・ int fd; ・ ・ if ( (fd = creat ("file.hole", 0644)) < 0) ・ err_sys ("create error"); ・ ・ if (write (fd, buf1, 10) != 10) ・ err_sys ("buf1 write error"); ・ ・ if (lseek (fd, 40960, SEEK_SET) == -1) ・ err_sys ("lseek error"); ・ ・ if (write (fd, buf2, 10) != 10) ・ err_sys ("buf2 write error"); ・ ・ exit (0); ・ } /*该程序取自<<unix的c编程>>,很简单也很经典的例程*/ [此贴被 夕阳(cimsxiyang) 在 5月6日14时0分 编辑过] |
地主 发表时间: 5/6 14:58 |
![]() | 回复: nightcolor [nightcolor] ![]() |
登录 |
推荐大家都看看哦,夕阳花了很大精力来完成,,呵呵,我也在学呢 |
B1层 发表时间: 05/06 13:34 |
![]() | 回复: cimsxiyang [cimsxiyang] ![]() |
登录 |
呵呵 现在我二眼浮肿,脑袋变大。 |
B2层 发表时间: 05/06 14:59 |
![]() | 回复: cimsxiyang [cimsxiyang] ![]() |
登录 |
icesky的评价不错,确实是最基础的东西,我会努力多写点程序和大家讨论。 在这里先谢谢icesky了。 |
B3层 发表时间: 05/06 15:16 |
![]() | 回复: top [top] ![]() |
登录 |
谢谢啊~~! 菜菜最喜欢这个了! 辛苦辛苦~~!呵呵~~! |
B4层 发表时间: 05/10 08:14 |
![]() | 回复: top [top] ![]() |
登录 |
O_CREA ・ O_NOCTTY ・ O_TRUNC ・ O_APPEND ・ O_NONBLOCK/O_NDELAY ・ O_SYNC ・ O_EXCL ・ 文件的访问许可: mode ・ S_IRWXU 00700 ・ S_IRUSR (S_IREAD) 00400 ・ S_IWUSR (S_IWRITE) 00200 ・ S_IXUSR (S_IEXEC) 00100 ・ ・ S_IRWXG 00070 ・ S_IRGRP 00040 ・ S_IWGRP 00020 ・ S_IXGRP 00010 ・ ・ S_IRWXO 00007 ・ S_IROTH 00004 ・ S_IWOTH 00002 ・ S_IXOTH 00001 这个不太明白!我是菜菜哈! |
B5层 发表时间: 05/10 08:20 |
![]() | 回复: ych7842369 [ych7842369] ![]() |
登录 |
![]() ![]() ![]() ![]() |
B6层 发表时间: 04-05-10 23:08 |
![]() | 回复: sniper167 [sniper167] ![]() |
登录 |
![]() |
B7层 发表时间: 04-05-11 10:17 |
![]() | 回复: yingzike [yingzike] ![]() |
登录 |
好东东,顶一下![]() ![]() ![]() |
B8层 发表时间: 04-05-12 10:03 |
![]() | 回复: afan271314 [afan271314] ![]() |
登录 |
(一)在哪呢 |
B9层 发表时间: 04-05-12 19:32 |
![]() | 回复: battle [battle] ![]() |
登录 |
![]() |
B10层 发表时间: 04-06-24 19:20 |
|
20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon
粤ICP备05087286号