2015年6月16日 星期二

[C] How to use mmap


$#include <stdio.h>$
$#include <fcntl.h>$
$#include <sys/types.h>$
$#include <sys/stat.h>$
$#include <unistd.h>$
$#include <sys/io.h>$
$#include <sys/mman.h>$

int main(int argc, char const *argv[])
{
unsigned char *f;
int size;
struct stat s;
const char * file_name = argv[1];
int fd = open (argv[1], O_RDONLY);
/* Get the size of the file. */
int status = fstat (fd, & s);
size = s.st_size;

f = (char *) mmap (0, size, PROT_READ, MAP_PRIVATE, fd, 0);
for (int i = 0; i < size; i++) {
    char c;

    c = f[i];
    putchar(c);
}

return 0;
}

Reference:
- Reading a file to string with mmap

0 意見:

張貼留言