Skip to content

Instantly share code, notes, and snippets.

@namazso
Created January 23, 2020 05:17
Show Gist options
  • Save namazso/330417af67e3fba9fb938f09ab472fe6 to your computer and use it in GitHub Desktop.
Save namazso/330417af67e3fba9fb938f09ab472fe6 to your computer and use it in GitHub Desktop.
Some helpers to handle large files with pure C
#define _CRT_SECURE_NO_WARNINGS
#define _FILE_OFFSET_BITS 64
#include <stdint.h>
#include <stdio.h>
/* while these are standard C functions used properly, your platform may require additional macros or
* settings to enable proper handling of files larger than 2 or 4 GB */
uint64_t large_getsize(FILE* fp)
{
fseek(fp, 0, SEEK_END);
uint64_t size = 0;
enum { gigabyte = 1 << 30 };
while (0 == fseek(fp, -1 * gigabyte, SEEK_CUR))
size += gigabyte;
size += ftell(fp);
rewind(fp);
return size;
}
void large_absseek(FILE* fp, uint64_t off)
{
rewind(fp);
enum { gigabyte = 1 << 30 };
while (off > gigabyte)
{
fseek(fp, gigabyte, SEEK_CUR);
off -= gigabyte;
}
fseek(fp, off, SEEK_CUR);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment