Чтение файла в строку с помощью mmap
Я пытаюсь прочитать файл в строку с помощью mmap.
Я следовал этому примеру:http://www.lemoda.net/c/mmap-example/index.html
Мой код выглядит так
unsigned char *f;
int size;
int main(int argc, char const *argv[])
{
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, 0, fd, 0);
for (i = 0; i < size; i++) {
char c;
c = f[i];
putchar(c);
}
return 0;
}
Но я всегда получаю ошибку сегментации при доступе к f [i]. Что я делаю неправильно?