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;
}

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;
}

You need to make use of the argc and and argv[] parameters.

If you type this in:

joinTextFiles chapter1.txt chapter2.txt chapter3.txt book.txt

then

argc = 5
argv[0] = "joinTextFiles"   // name of executable
argv[1] = "chapter1.txt"   // name of input file
argv[2] = "chapter2.txt"   // name of input file
argv[3] = "chapter3.txt"   // name of input file
argv[4] = "book.txt"        // name of output file

You don't want to hardcode in lines 10 through 13 and elsewhere because these will change. You can have multiple ifstream variables like you do (you're using fstream, not ifstream), calling them chapter_one, chapter_two, chapter_three. If you go this route, you'll want to have an array of ifstreams. Or you can have a single ifstream and continuously use it with different filenames. You don't need to set up an array of filenames since that's done for you (it's called argv). Access argv[] as you would any other array. Ignore argv[0] in this program since it is of no use.

#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    cout << "There are " << argc - 2 << " input files." << endl;
    for (int i = 1; i < argc - 1; i++)
        cout << "Input file " << i << " = " << argv[i] << endl;
        
    cout << "Output file = " << argv[argc - 1] << endl;

    return 0;
}

worked, perfectly. Thanks alot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.