C Program for splitting the given file into fixed size blocks
// split.cpp : main project file.
#include "stdafx.h"
using namespace System;
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define SEGMENT 600 //approximate target size of small file
long file_size(char *name);//function definition below
int splitFile(char *fp1, size_t maxSize);
long file_size(char *name)
{
FILE *fp = fopen(name, "rb"); //must be binary read to get bytes
long size=-1;
if(fp)
{
fseek (fp, 0, SEEK_END);
size = ftell(fp)+1;
fclose(fp);
}
return size;
}
/*
** splitFile
** Splits an existing input file into multiple output files with a specified
** maximum file size.
**
** Return Value:
** Number of created result files, or 0 in case of bad input data or a negative
** value in case of an error during file splitting.
*/
int splitFile()
{
int result = 0;
char buffer[1024];//change see //2098 * 16
size_t size;
size_t read;
size_t written;
int segments=0, i, len, accum;
//FILE *fp1, *fp2;
char filename[260]={""};
//char filename[260]={"D:\\smallFileName_"};//base name for small files.
char largeFileName[]={"D:\\Payload_data\\Datafile_to_split.txt"};//change to your path
long sizeFile = file_size(largeFileName);
char smallFileName[260];
char line[1024];
long maxSize= sizeFile/SEGMENT + 1;
int filecounter=1;
FILE *fIn;
FILE *fOut;
fIn = fopen(largeFileName, "rb");
if ((fIn != NULL) && (maxSize > 0))
{
// fIn = fopen(fIn, "rb");
if (fIn != NULL)
{
fOut = NULL;
result = 1; /* we have at least one part */
// Splitting Data from pchar into multiple files
for(i=1; i<25 ;i++)
{
sprintf(smallFileName, "%s%d.txt", filename, i);
printf("\n File number %d",i);
while (!feof(fIn))
{
/* initialize (next) output file if no output file opened */
if (fOut == NULL)
{
filecounter++;
sprintf(buffer, "smallFileName_%d", filecounter);
fOut = fopen(buffer, "wb");
if (fOut == NULL)
{
result *= -1;
break;
}
size = 0;
}
/* calculate size of data to be read from input file in order to not exceed maxSize */
read = sizeof(buffer);
if ((size + read) > maxSize)
{
read = maxSize - size;
}
/* read data from input file */
read = fread(buffer, 1, read, fIn);
if (read == 0)
{
result *= -1;
break;
}
/* write data to output file */
written = fwrite(buffer, 1, read, fOut);
if (written != read)
{
result *= -1;
break;
}
/* update size counter of current output file */
size += written;
if (size >= maxSize) /* next split? */
{
fclose(fOut);
fOut = NULL;
result++;
}
}
/* clean up */
if (fOut != NULL)
{
fclose(fOut);
}
fclose(fIn);
}
}
}
return (result);
}
int main(void)
{
//segments = sizeFile/SEGMENT + 1;//ensure end of file
//fp1 = fopen(largeFileName, "r");
////if(fp1)
////{
// for(i=0;i<segments;i++)
// {
// accum = 0;
// sprintf(smallFileName, "%s%d.txt", filename, i);
// fp2 = fopen(smallFileName, "w");
// if(fp2)
// {
// while(fgets(line, 1080, fp1) && accum <= SEGMENT)
// {
// accum += strlen(line);//track size of growing file
// fputs(line, fp2);
// }
// fclose(fp2);
// }
// }
// fclose(fp1);
//}
//maxSize =sizeFile/SEGMENT + 1;
//ensure end of file
splitFile();
getch();
printf("\n File splitted Successfully");
return 0;
}