随笔 - 56, 文章 - 0, 评论 - 0, 引用 - 0
数据加载中……

底层文件访问

1、lseek系统调用
    #include<unistd.h>
    #include<sys/types.h>
   
    off_t lseek(int fildes,off_t offset,int whence);
    whence可以取下面其一:
    SEEK_SET: offset是一个绝对位置。
    SEEK_CUR: offset是相对于当前位置的一个相对位置。
    SEEK_END: offset是相对于文件尾到一个相对位置。
    返回从文件头到文件指针被设置处到字节偏移值,失败时返回-1。
    例子:
 1     #include<unistd.h>
 2     #include<stdlib.h>
 3     #include<stdio.h>
 4     #include<sys/types.h>
 5     #include<fcntl.h>
 6     #include<sys/stat.h>
 7 
 8     int main()
 9     {
10         int fd;
11         off_t off;
12 
13         fd=open("./text.txt",O_RDWR);
14         if(fd==-1)
15         {
16             write(2,"fail to open!\n",13);
17             exit(1);
18         }
19 
20         write(1,"locating!\n",10);
21         //off=lseek(fd,5,SEEK_CUR);     //可以轮番测试
22         off=lseek(fd,0,SEEK_END);       //off_set为0,所以在text.txt文档末尾添加内容。
                                                                        如果off_set为1,则在文档末尾到1个字符后面
                                                                        插入内容。
23         write(1,"Writting!\n",10);
24         printf("%d\n",off);
25         if(write(fd,"hello\n",6)==-1)
26         {
27             write(2,"fail to write!\n",14);
28             exit(1);
29         }
30 
31         close(fd);
32 
33         exit(0);
34     }

2、dup和dup2系统调用
    #include<unistd.h>
    
    int dup(int fildes);
    int dup2(int fildes,int fildes2);
   
dup:
    dup系统调用提供了一种复制文件描述符的方法,使我们能够通过两个或者更多个不同到描述符来访问同一个文件。dup复制文件描述符fildes,返回
一个新的描述符。
    例子:
 1     #include<stdio.h>
 2     #include<unistd.h>
 3     #include<fcntl.h>
 4     #include<stdlib.h>
 5 
 6     int main()
 7     {
 8         int fd;
 9 
10         fd=open("./text.txt",O_RDWR);
11         if(fd==-1)
12         {
13             perror("fail to open.");
14             exit(1);
15         }
16         else
17             printf("open ok\n");
18    
19         if(close(1)==-1)           //关掉文件描述符1(标准输出)后,则当前最小
                                                      到文件描述符为1,因为0没有关掉。

20         {
21             perror("fail to close.\n");
22             exit(1);
23         }
24    
25         if(dup(fd)==-1)           //使用dup后,返回当前最小到文件描述符1,所以用printf
                                                      函数打印到内容本来在屏幕上到现在转到text.txt中。

26         {     
27             perror("fail to dup.\n");
28             exit(1);
29         }
30         else
31             printf("Dup OK\n");
32    
33         printf("hello world.\n");
34    
35         exit(0);
36     }    

   
dup2:
    dup2系统调用则通过明确指定目标描述符来把一个文件描述符复制到另一个。
    dup2函数跟dup函数相似,但dup2函数允许调用者规定一个有效描述符和目标描述符的id。dup2函数成功返回时,目标描述符(dup2函数的第二个参数)将变成源描述符(dup2函数的第一个参数)的复制品,换句话说,两个文件描述符现在都指向同一个文件,并且是函数第一个参数指向的文件。下面我们用一段代码加以说明:
 1     #include<stdio.h>
 2     #include<unistd.h>
 3     #include<fcntl.h>
 4     #include<stdlib.h>
 5 
 6     int main()
 7     {
 8         int fd;
 9    
10         fd=open("./text.txt",O_RDWR);
11         if(fd==-1)
12         {
13             perror("fail to open.\n");
14             exit(1);
15         }
16         else
17             printf("open OK\n");
18    
19         if(dup2(fd,1)==-1)         //现在1重定向到text.txt,所有到printf输出都重定向到该文件。
20         {
21             perror("fail to dup");
22             exit(1);
23         }
24         else
25             printf("dup2 STDOUT OK.\n");
26    
27         printf("hello world!\n");
28    
29         exit(0);
30     } 

posted on 2011-03-03 19:10 八路 阅读(255) 评论(0)  编辑 收藏 引用 所属分类: 嵌入式linux笔记


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理