使用mmap在内存中读写文件

本文介绍如何使用mmap将文件映射到内存中,并通过字符串函数实现文件内容的修改。测试代码展示了插入字符串的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通常情况下是使用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是不能修改文件的长度的。 

转载于:https://www.cnblogs.com/java-koma/archive/2012/12/01/2797226.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值