Created
February 17, 2015 21:15
-
-
Save pekkavaa/530c224927687400cfcd to your computer and use it in GitHub Desktop.
Splits binary files in two parts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define FTELL _ftelli64 | |
#define FSEEK _fseeki64 | |
void usage() | |
{ | |
puts("parts FILE OUTPUT OFFSET [LENGTH]"); | |
} | |
void die(const char* msg) | |
{ | |
puts(msg); | |
exit(1); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
if (argc < 4) { | |
usage(); | |
return 1; | |
} | |
const char* path = argv[1]; | |
const char* output_path = argv[2]; | |
unsigned long long offset = atoi(argv[3]); | |
unsigned long long length = 0; | |
FILE* fp = fopen(path, "rb"); | |
if (!fp) die("Couldn't open input file."); | |
FILE* out = fopen(output_path, "wb"); | |
if (!out) die("Couldn't open output file."); | |
FSEEK(fp, 0, SEEK_END); | |
unsigned long long size = FTELL(fp); | |
FSEEK(fp, offset, SEEK_SET); | |
if (argc < 5) { | |
length = size - offset; | |
} else { | |
length = atoi(argv[4]); | |
} | |
char* data = malloc(length); | |
if (fread(data, sizeof(char), length, fp) != length) { | |
puts("Couldn't read data."); | |
goto end; | |
} | |
if (fwrite(data, sizeof(char), length, out) != length) { | |
puts("Couldn't write data."); | |
} | |
free(data); | |
end: | |
fclose(fp); | |
fclose(out); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment