linux编程学习笔记(九) 获取文件状态与文件映射mmap

原地址:http://blog.csdn.net/a8887396/article/details/9009485


1 fstat 获取文件状态

     int fstat(int fd, struct stat *buf);


         struct stat {
               dev_t     st_dev;     /* ID of device containing file */
               ino_t     st_ino;     /* inode number */
               mode_t    st_mode;    /* protection */
               nlink_t   st_nlink;   /* number of hard links */
               uid_t     st_uid;     /* user ID of owner */
               gid_t     st_gid;     /* group ID of owner */
               dev_t     st_rdev;    /* device ID (if special file) */
               off_t     st_size;    /* total size, in bytes */
               blksize_t st_blksize; /* blocksize for file system I/O */
               blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
               time_t    st_atime;   /* time of last access */
               time_t    st_mtime;   /* time of last modification */
               time_t    st_ctime;   /* time of last status change */
           };


2 ftruncate改变文件大小

       int ftruncate(int fd, off_t length);
如果length比原来的大,则在文件后面添加'\0'

如果length比原来的小,则在截断文件


[cpp]  view plain copy
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3.  #include <fcntl.h>  
  4. #include <unistd.h>  
  5. #include <stdio.h>  
  6.   
  7.   
  8. int main()  
  9. {  
  10.     int fd;  
  11.     struct stat st;  
  12.     fd = open("stu.dat",O_RDWR);  
  13.     fstat(fd,&st);  //错误:存储大小未知  没加头文件  
  14.     printf("%d,%o\n",(int)st.st_size,st.st_mode); //72 100644  
  15.       
  16.       
  17.     ftruncate(fd,st.st_size+1000); // 需要写权限才能改变大小  
  18.     fstat(fd,&st);  
  19.     printf("%d\n",(int)st.st_size);  
  20.     close(fd);  
  21.       
  22. }  


zhao@ubuntu:~/unix/4$ ./fstat 
72,100644
1072


3  文件映射

mmap /munmap

之前介绍过(http://blog.csdn.net/a8887396/article/details/8996213) ,写的是内存映射,拷过来

void *mmap(
void *start, //指定映射的虚拟地址  如为0 系统指定开始位置
size_t length, //映射的空间大小 : pagesize倍数
int prot,//映射权限 PROT_NONE PROT_READ PROT_WRITE PROT_EXEC
int flags,//映射方式
int fd,//文件描述符
offset_t  off); //文件中的偏移位置(必须是page_size的倍数)
       int munmap(void *addr, size_t length);
       映射方式:
内存映射:匿名映射
文件映射:映射到文件 ,只有当文件映射时,最后两个参数才有效
MAP_ANONYMOUS 写了就是内存映射 不写就是文件映射
MAP_PRIVATE MAP_SHARED 2选1

  umap(void *start,size_t lenth)

案例:
1使用内存方式写入数据

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <fcntl.h>  
  6. #include <unistd.h>  
  7. #include <string.h>  
  8. #include <sys/mman.h>  
  9.   
  10.   
  11. struct stu  
  12. {  
  13.     char name[20];  
  14.     int age;  
  15.     float score;  
  16. };  
  17.   
  18. int main()  
  19. {  
  20.     //打开文件  
  21.     //增加文件大小  
  22.     //映射到虚拟地址  
  23.     //把数据写入虚拟地址  
  24.     //卸载虚拟地址  
  25.     //关闭文件  
  26.       
  27.     int fd = open("newstu.dat",O_RDWR|O_CREAT,0666);  
  28.     if(fd < 0)  
  29.     {  
  30.         perror("open error");  
  31.         return 1;  
  32.     }  
  33.     struct stat st;  
  34.     fstat(fd,&st);  
  35.     int size = st.st_size ; //文件大小  
  36.     int count = size/sizeof(struct stu);//记录条数  
  37.     //因为要增加数据 所以要先增加文件大小(很重要)  
  38.     ftruncate(fd,size+sizeof(struct stu)); //增加文件大小  
  39.       
  40.     //将文件映射到内存的虚拟地址,得到文件在虚拟内存中映射的首地址   
  41.     struct stu*s= mmap(0,  
  42.                size+sizeof(struct stu),  
  43.                PROT_WRITE|PROT_READ,  
  44.                MAP_SHARED,  
  45.                fd,0);  
  46.   
  47.     printf("请输入学生姓名");  
  48.     scanf("%s",s[count].name);  
  49.     printf("请输入学生年龄");  
  50.     scanf("%d",&s[count].age);  
  51.     printf("请输入学生成绩");  
  52.     scanf("%f",&s[count].score);  
  53.     munmap(s,size+sizeof(struct stu));  
  54.       
  55.     close(fd);  
  56.       
  57. }  


2使用内存方式读取数据

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <fcntl.h>  
  6. #include <unistd.h>  
  7. #include <string.h>  
  8. #include <sys/mman.h>  
  9.   
  10. struct stu  
  11. {  
  12.     char name[20];  
  13.     int age;  
  14.     float score;  
  15. };  
  16.   
  17. int main()  
  18. {  
  19.     int fd = open("newstu.dat",O_RDWR);  
  20.     if(fd < 0)  
  21.     {  
  22.         perror("open fail");  
  23.         return 1;  
  24.     }  
  25.       
  26.     struct stat st;  
  27.     fstat(fd,&st);  
  28.     int size = st.st_size;  
  29.     int count = size/sizeof(struct stu);  
  30.       
  31.     struct stu*s = mmap(0,size,PROT_READ | PROT_WRITE,  
  32.         MAP_SHARED,fd,0);  
  33.     if(s < 0)  
  34.     {  
  35.         perror("mmap error");  
  36.         return 1;  
  37.     }  
  38.       
  39.     int i =0;  
  40.     for(; i<count ;i++)  
  41.     {  
  42.         printf("name:%s,age:%d,score:%.2f\n",s[i].name,s[i].age,s[i].score);  
  43.     }  
  44.       
  45.       
  46.     munmap(s,size);  
  47.     close(fd);  
  48.       
  49. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值