How to split a file other that .txt extension using C++ code

I've been looking for a solution to split a file with any file format, using C++. what i've found so far and tried as well, could only split .txt file. To split (say .docx) file into 2 files is possible, but to open splitted files give error. The same is true with other file formats. For example try the the code below with a different file extension rather than .txt or .dat (say .jpg).

is anyone who have a solution?

Best Regards,

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>

using namespace std;
int main()
{
ifstream fin1, fin2;
ofstream fout;
char ch, file_name1[20], file_name2[20], file_name3[30];
cout<<"\n Enter First File Name with Extension !=.txt : ";
gets(file_name1);
cout<<"\n Enter Second File Name with Extension !=.txt : ";
gets(file_name2);
cout<<"\n Enter Third File Name with Extension !=.txt ";
cout<<"\n (which will Store the Contents of \n First File and Second File) : ";
gets(file_name3);

fin1.open(file_name1);
fin2.open(file_name2);
if(fin1==NULL || fin2==NULL)
{
cout<<"\n Invalid File Name. \n There is no such File ...";
exit(EXIT_FAILURE);
}
fout.open(file_name3);
if(!fout)
{
cout<<"\n Invalid File Name. \n There is no such File...";
exit(EXIT_FAILURE);
}
while(fin1.eof()==0)
{
fin1>>ch;
fout<<ch;
}
Last edited on
Word, excel, pdf's have special formatting you have to consider.
you can split it easy enough -- we used to do that to fit big files on small disks (even modern compression tools like 7zip retain this function), when a disk held less than 2 mb. But you can't open a split file in program that can read it -- the 2nd half of a word file is just gibberish to word, because the headers and things in the first half are missing. Same for these others. All you can do with a split file is put it back together and THEN open it.

you can maybe split some of these specifically but you won't be able to generally split any file into WORKING file pieces. It does not work that way. It doesn't even work that way on specifically formatted text files!

Topic archived. No new replies allowed.