Here is my question
Write a command line program titled joinTextFiles.cpp that concatenates the contents of several files into one file. For example, the following command line
joinTextFiles chapter1.txt chapter2.txt chapter3.txt book.txt
will create a long file titled book.txt that contains the contents of the files chapter1.txt, chapter2.txt, and chapter3.txt. The target file, the one containing the contents of the other three files, is always the last file specified on the command line.
--------------------------------------------------------------------
I have written all the code, and it works in the compiler. The textfile's join and create "book.txt". I just don't know how to do it as an executable.
can any1 help? Here is my code.
#include <iostream> //header files needed for the function and vector
#include <string>
#include <fstream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) // declaration of all variables
{
string line;
fstream chapter_one ("chapter1.txt"); //open the 3 text files
fstream chapter_two ("chapter2.txt");
fstream chapter_three ("chapter3.txt");
ofstream book ("book.txt"); //creates book.txt
if (chapter_one.is_open()) //if file chapter_one opens
{
while (! chapter_one.eof() ) //while NOT end of chapter1.txt
{
getline (chapter_one, line); //get contents of chapter1.txt
book << line << endl; //output contents of chapter1.txt to book.txt
}
while (! chapter_two.eof() ) //while NOT end of chapter2.txt
{
getline (chapter_two, line); //get contents of chapter2.txt
book << line << endl; //output contents of chapter2.txt to book.txt
}
while (! chapter_three.eof() ) //while NOT end of chapter3.txt
{
getline (chapter_three, line); //get contents of chapter3.txt
book << line << endl; //output contents of chapter3.txt to book.txt
}
cout <<"The files chapter1.txt, chapter2.txt and chapter3.txt. have all been joined\ntogether and outputted in the file book.txt.\n";
}
else cout <<"Unable to open file.\n"; //user error message
chapter_one.close();
chapter_two.close();
chapter_three.close();
book.close();
return 0;
}