Skip to content

Instantly share code, notes, and snippets.

@ilyabrin
Created June 14, 2013 08:10
Show Gist options
  • Select an option

  • Save ilyabrin/5780270 to your computer and use it in GitHub Desktop.

Select an option

Save ilyabrin/5780270 to your computer and use it in GitHub Desktop.
C++ read big files
// source -> http://stackoverflow.com/a/11792512
#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif
int read_from_file_open(char *filename,size_t size)
{
int fd;
long *buffer=(long*) malloc(size * sizeof(long));
fd = open(filename, O_RDONLY|O_LARGEFILE);
if (fd == -1)
{
printf("\nFile Open Unsuccessful\n");
exit (0);;
}
off_t chunk=0;
lseek(fd,0,SEEK_SET);
printf("\nCurrent Position%d\n",lseek(fd,size,SEEK_SET));
while ( chunk < size )
{
printf ("the size of chunk read is %d\n",chunk);
size_t readnow;
readnow=read(fd,((char *)buffer)+chunk,1048576);
if (readnow < 0 )
{
printf("\nRead Unsuccessful\n");
free (buffer);
close (fd);
return 0;
}
chunk=chunk+readnow;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment