Вы можете использовать карту памяти io с mmap . Вот пример чтения файла / etc / fedora-release и печати его содержимого:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define handle_error(_label, _text) do { perror(_text); goto _label; } while (0)
int main(int argc, char *argv[])
{
char *addr;
int fd;
struct stat sb;
size_t length;
ssize_t s;
fd = open("/etc/fedora-release", O_RDONLY);
if (fd == -1) {
handle_error(exit_failure, "open");
}
if (fstat(fd, &sb) == -1) {
handle_error(exit_failure, "fstat");
}
length = sb.st_size;
addr = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
if (addr == MAP_FAILED) {
handle_error(exit_failure, "mmap");
}
s = write(STDOUT_FILENO, addr, length);
if (s != length) {
if (s == -1) {
handle_error(exit_failure, "write");
}
fprintf(stderr, "partial write");
goto exit_failure;
}
exit(EXIT_SUCCESS);
exit_failure:
exit(EXIT_FAILURE);
}