Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h> //Has issues on PS3
typedef struct {
char* sFileName;
int iMain_Parts;
int iLast_Part;
long lMain_Remainder;
long lLast_Remainder;
int file_parts;
} file_details;
void read_write_file(char * sFileName_Part,char * buffer,long chunk_size_total,long chunk_size_total2, char * sFileName, int iPlace) {
FILE * f_File_Final;
FILE * f_File_Piece;
f_File_Piece = fopen ( sFileName_Part, "rb" );
fseek ( f_File_Piece , (iPlace*chunk_size_total) , SEEK_SET );
fread (buffer,1,chunk_size_total2,f_File_Piece);
fclose (f_File_Piece);
f_File_Final = fopen ( sFileName , "ab" );
fwrite (buffer , 1 , chunk_size_total2 , f_File_Final );
fclose (f_File_Final);
}
void file_merge(file_details p) {
char sFileName_Part[1024];
//long chunk_size_mb = 5;
long chunk_size_total = 50000000;
//Buffer Data
char * buffer = (char*) malloc (sizeof(char)*chunk_size_total);
char * buffer_part = (char*) malloc (sizeof(char)*p.lMain_Remainder);
char * buffer_last_part = (char*) malloc (sizeof(char)*p.lLast_Remainder);
for ( int x = 0; x <= p.file_parts-1; x++ ){
sprintf(sFileName_Part, "%s.%.03d", p.sFileName,x+1);
if (x < p.file_parts-1) {
for ( int y = 0; y <= p.iMain_Parts; y++ ) {
if (y < p.iMain_Parts) {
read_write_file(sFileName_Part,
buffer,
chunk_size_total,
chunk_size_total,
p.sFileName,y);
} else {
if (p.lMain_Remainder>0) {
read_write_file(sFileName_Part,
buffer_part,
chunk_size_total,
p.lMain_Remainder,
p.sFileName,y);
}
}
}
}
char sOutPut[1024];
sprintf(sOutPut,"x = %i -- File parts = %i \n",x,p.file_parts);
printf(sOutPut);
if (x == p.file_parts-1) {
for ( int y = 0; y <= p.iLast_Part; y++ ) {
if (y < p.iLast_Part) {
read_write_file(sFileName_Part,
buffer,
chunk_size_total,
chunk_size_total,
p.sFileName,y);
} else {
if (p.lLast_Remainder>0) {
read_write_file(sFileName_Part,
buffer_last_part,
chunk_size_total,
p.lLast_Remainder,
p.sFileName,y);
}
}
}
}
}
free (buffer); //disengages the buffer - I should probably not use this
}
void file_part_details (char* sFileName) {
//This will do all the mesurements for merging so they only need to be done once.
//int iParts_total;
long lMain_Part_Count; //How many 5meg parts per full file
long lMain_Remainder; //Remainder on each full file
long lLast_Part_Count; //How many 5meg parts per last file
long lLast_Remainder; //Remainder on last file
long lCurrent_Part_Count;
long lCurrent_Remainder;
char sFileNameExt[1024];
char sFileName_Part[1024];
int iLoopCount = 1;
FILE * f_File_Piece;
long chunk_size_total = 5000000;
long lSize;
int x = 0;
int y = 0;
while ( x == 0) {
sprintf(sFileNameExt, "%.03d", iLoopCount);
sprintf(sFileName_Part, "%s.%s", sFileName,sFileNameExt);
if ( (f_File_Piece = fopen ( sFileName_Part, "rb" ) ) == NULL ) {
x=1;
lLast_Part_Count = lCurrent_Part_Count;
lLast_Remainder = lCurrent_Remainder;
} else {
iLoopCount++;
// obtain file size:
fseek (f_File_Piece , 0 , SEEK_END);
lSize = ftell (f_File_Piece);
fclose (f_File_Piece);
lCurrent_Part_Count = 0;
lCurrent_Remainder = 0;
lCurrent_Part_Count = (lSize / chunk_size_total);
lCurrent_Remainder = lSize - (lCurrent_Part_Count*chunk_size_total);
if (y == 0) {
lMain_Part_Count = lCurrent_Part_Count;
lMain_Remainder = lCurrent_Remainder;
y++;
}
}
}
file_details p = {
sFileName,
lMain_Part_Count,
lLast_Part_Count,
lMain_Remainder,
lLast_Remainder,
iLoopCount-1};
file_merge(p);
}
Edit: