C++ Program to Split file into 3 equal files.
This program will take input from data.txt file and split it into 3 equal size files (data1.txt, data2.txt, data3.txt).
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout1, fout2, fout3;
char ch;
int i, cnt=0, equal_part=0, last_part=0;
fin >> std::noskipws;
fin.open("data.txt");
if(!fin){
cout<<"Error Open Error."; exit(1);
}
//logic to count total character in input data file
while(fin.eof()==0){
fin >> ch;
cnt++;
}
cout << "Total characters in data.txt file = "<< cnt-1 << endl;
fin.close();
//logic to divide no. characters in each file
equal_part = (cnt-1)/3;
last_part = (cnt-1)%3;
cout <<"Total files to be created=3."<< endl;
cout << "Each output file contains: "<<equal_part <<" characters."<<endl;
cout << "Last output file contains: "<<equal_part+last_part<<" characters."<<endl;
fin.open("data.txt");
fin >> std::noskipws;
fout1.open("data1.txt");
if(!fout1){
cout<<"File data1.txt Open Error."; exit(1);
}
for(i=0; i<equal_part; i++) {
fin >> ch;
fout1 << ch;
}
fout2.open("data2.txt");
if(!fout2){
cout<<"File data2.txt Open Error."; exit(1);
}
for(i=0; i<equal_part; i++)
{
fin >> ch;
fout2 << ch;
}
fout3.open("data3.txt");
if(!fout3){
cout<<"File data3.txt Open Error."; exit(1);
}
for(i=0; i<equal_part+last_part; i++)
{
fin >> ch;
fout3 << ch;
}
fin.close();
fout1.close();
fout2.close();
fout3.close();
return 0;
}
Output of program
Total characters in data.txt file = 10
Total files to be created=3.
Each output file contains: 3 characters.
Last output file contains: 4 characters.
This program will take input from data.txt file and split it into 3 equal size files (data1.txt, data2.txt, data3.txt).
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout1, fout2, fout3;
char ch;
int i, cnt=0, equal_part=0, last_part=0;
fin >> std::noskipws;
fin.open("data.txt");
if(!fin){
cout<<"Error Open Error."; exit(1);
}
//logic to count total character in input data file
while(fin.eof()==0){
fin >> ch;
cnt++;
}
cout << "Total characters in data.txt file = "<< cnt-1 << endl;
fin.close();
//logic to divide no. characters in each file
equal_part = (cnt-1)/3;
last_part = (cnt-1)%3;
cout <<"Total files to be created=3."<< endl;
cout << "Each output file contains: "<<equal_part <<" characters."<<endl;
cout << "Last output file contains: "<<equal_part+last_part<<" characters."<<endl;
fin.open("data.txt");
fin >> std::noskipws;
fout1.open("data1.txt");
if(!fout1){
cout<<"File data1.txt Open Error."; exit(1);
}
for(i=0; i<equal_part; i++) {
fin >> ch;
fout1 << ch;
}
fout2.open("data2.txt");
if(!fout2){
cout<<"File data2.txt Open Error."; exit(1);
}
for(i=0; i<equal_part; i++)
{
fin >> ch;
fout2 << ch;
}
fout3.open("data3.txt");
if(!fout3){
cout<<"File data3.txt Open Error."; exit(1);
}
for(i=0; i<equal_part+last_part; i++)
{
fin >> ch;
fout3 << ch;
}
fin.close();
fout1.close();
fout2.close();
fout3.close();
return 0;
}
Output of program
Total characters in data.txt file = 10
Total files to be created=3.
Each output file contains: 3 characters.
Last output file contains: 4 characters.