11 Sep 12, 07:32PM
Reminds me of a C++ program I wrote.
I made all of its comments haiku for some reason.
I made all of its comments haiku for some reason.
[SELECT ALL] Code:
#include <iostream>
#include <fstream>
#include <vector>
#include "..\Combinator\convtime.h"
#define BRATE 616628
using namespace std;
int main(int argc, char* argv[])
{
cout<<"Text file splitter v. 0.92, 2-26-2012" <<endl;
if (argc < 2) // Function takes 2 args
{
cout<<"Usage:" <<endl; // Otherwise, explains itself
cout<<"txtsplit [File Name] [# Strings Per File]" <<endl;
cout<<"txtsplit EXAMPLE.TXT 1000000" <<endl;
cout<<"Splits EXAMPLE.TXT into files containing up to 1000000 strings each" <<endl;
return 0; // Please come back later
}
ifstream file (argv[1], ios::ate); // Open the target
if (!file.is_open()) { // Make sure it really exists
cout<<"File not found: " <<argv[1] <<endl; // Elusive, like wind
return 1;
}
cout<<"File " <<argv[1] <<" opened" <<endl;
cout<<file.tellg() <<" bytes" <<endl;
cout<<"This will take about " <<convtime((file.tellg()) / BRATE) <<endl;
file.seekg(0, ios::beg);
cout<<"Press Enter when ready" <<endl; // This tells the user
cin.get(); // Proceed now, or chicken out?
vector<string> readfile; // This vector will read
cout<<"Splitting..." <<endl;
char ebuf[8];
string str, buffer, ofile, ifile = (argv[1]); // 4 strings: all just names
unsigned int i = 0; // Introduce the file counter
while (file.good()) // Check for end of file
{
while (
((readfile.size()) < (atoi(argv[2]))) // Vector is not full,
&& (file.good())) { // And the file has not ended,
file>>str; // Take a string from it
readfile.push_back(str);} // Insert in vector
ofile = (_itoa (i,ebuf,10)); // Change the string to a number
ofile += "_" + ifile; // Append the filename
ofstream outputfile (ofile, ios::trunc); // Create this new file
for (unsigned j=0; j < readfile.size(); j++) { // For each string in the vector,
outputfile<<(readfile.at(j)) + "\n"; // Write to the output
}
outputfile.close(); // Close the outputfile
readfile.clear(); // Now empty the read buffer
i++; // Increment the count
} // This loop has ended
file.close(); // It is time to stop reading
return 0; // The split is complete
}