Skip to content

Instantly share code, notes, and snippets.

@douglas-vaz
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglas-vaz/9407105 to your computer and use it in GitHub Desktop.
Save douglas-vaz/9407105 to your computer and use it in GitHub Desktop.
Fast file read in Linux
#include "linux_io.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <wchar.h>
int main(void)
{
char *buffer;
size_t l;
int i;
buffer = (char*)malloc(BUFFER_SIZE + 1);
int fd;
fd = open("../../dataset/dataset.txt", O_RDONLY);
l = f_read(fd, buffer);
printf("Bytes read: %lu\n",l);
printf("Bytes printed: %lu\n", strlen(buffer));
printf("%lu\n", strlen(buffer));
printf("%s\n", buffer);
free(buffer);
return 0;
}
#ifndef __LINUX_IO__
#define __LINUX_IO__
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef __linux__
#include <unistd.h>
#include <fcntl.h>
#endif
//Consts
#define BLKSIZE 4096
#define BUFFER_SIZE (2 * 4096)//(16 * 1024)
size_t f_read(int, char*);
/**
* Reads a file specified by it's file descriptor and stores contents into a PRE-ALLOCATED buffer
* Return value: total number of bytes read
*/
size_t f_read(int fd, char *buffer)
{
//Get file descriptor by 'open' system call. -1 indicates error
if(fd == -1)
return fd;
else
{
#ifdef __linux__
//Advise the kernel on read for optimizations
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
size_t bytes_read;
size_t total_bytes = 0;
while((bytes_read = read(fd, buffer, BUFFER_SIZE)))
{
if(!bytes_read)
break;
total_bytes += bytes_read;
}
buffer[BUFFER_SIZE] = '\0';
//printf(L"%s\n", buffer);
#endif
return total_bytes;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment