使用mmap在内存中读写文件


通常情况下是使用read/write,fread/fwrite等来读写文件,linux提供了一种方式可以将文件映射到内存,然后可以用字符串的方式对它进行读写操作,并写回到文件。 

下面是一段测试代码,目的: 用mmap将文件abc.txt映射到内存,利用字符串函数向该内存中插入一个字符串,以达到修改文件的目的。

 

 

 #include <stdio.h>

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include < string.h>

#define handle_error(msg) do{perror(msg);exit(EXIT_FAILURE);} while(0)

int main( int argc,  char *argv[])
{
         int fd;
        off_t length;
         char *addr;
         char *inserted =  " ## inserted ## "//  this str will be inserted to the file
         int pos =  5//  the position to insert
        fd = open( " abc.txt ", O_RDWR | O_CREAT,  0644);
         if(fd == - 1)
        {
                handle_error( " open file error ");
        }
        length = lseek(fd,  1, SEEK_END);
        write(fd,  " \0 "1);
        addr = ( char *)mmap(NULL, length + strlen(inserted), PROT_READ | PROT_WRITE, MAP_SHARED, fd,  0);
        memcpy(addr + pos + strlen(inserted), addr + pos, length - pos);
        memcpy(addr + pos, inserted, strlen(inserted));
         // printf("addr: %s", addr);
        close(fd);
        munmap(( void *)addr, length);
}

 

运行:

zhangxd@ubuntu:~/linux$ cat abc.txt 
This is the first message.
zhangxd@ubuntu:~/linux$ ./test 
zhangxd@ubuntu:~/linux$ cat abc.txt 

This ## inserted ##is the fir 

 

可以看到文件内容已经被修改了,细心的朋友会发现,mmap时申请的长度大于文件的长度,但是最终文件的长度并没有改变,由此得出结论:mmap是不能修改文件的长度的。 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM