Linux C:磁盘文件映射到内存/mmap()

一、代码

        #include

        mmap()

1.1 文件myfile

        需要事先准备好的文件内容:


1.2 代码

#include 
#include 
#include 

int main(int argc, char*argv[])
{
        int fd = open("myfile", O_RDWR);
        if (fd < 0)
        {
                perror("open file error");
                exit(1);
        }

        int *p = mmap(NULL, 6, PROT_WRITE, MAP_SHARED, fd, 0);
        if (p == MAP_FAILED)
        {
                perror("mmap error");
                exit(1);
        }

        close(fd);

        p[0] = 0x30313233;
        munmap(p, 6);

        return 0;
}

二、输出结果

执行完后,再查看文件myfile:


你可能感兴趣的:(IO/文件读写)