Alright so i have this assignment to do. The question is as follow's.
------------------------------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.

------------------------------Example------------------------------
Im not sure about how to do this all in a single command line. Ive managed to get this done without creating a command line. My code is here.

#include <iostream> //header files needed for the function and vector
#include <string>
#include <fstream>
using namespace std;

int main() // 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;
}

The problem is that i cant find a way to do it the way the question is asking for. I have searched the internet and cant find anything. Could i get some help on this please.

main() has two optional arguments that are the number of command line arguments and an array of strings are are the arguments

int main(int argc, char* argv[])
{

}

argc is always >= 1 because argv[0] is the name of the executable program.
In your example joinTextFiles chapter1.txt chapter2.txt chapter3.txt book.txt argv[0] == "joinTextFiles"
argv[1] == chapter2.txt
argv[2] == chapter3.txt
argv[3] == book.txt

Now in your progrsam just substitute argv[1], argv[2], ... for the hard-coded names in the open statements.

commented: Making the previous rep count. +14

your help is greatly appreciated. I will try it out now and see if it works.

Alright, i cant get this to work. Im not sure what u mean by substitute argv[1], argv[2], ... for the hard-coded names in the open statements.

Sorry, but i am very new to c++, only been using it for a few weeks now so i will need more precise instructions if you could pls.

hi

you may try this

int main(int argc, char *argv[]){
   cout << "Program name: " << argv[0] << endl;
   cout << "Numbers of Parameters: " << argc << endl << "Parameters:\n";
   for(int i=1; i<argc;i++) cout << i << "  " << argv[i] << endl;
   return 0;
}
/* result in console window:
C:\>cara a b c d e f
Program name: cara
Numbers of Parameters: 7
Parameters:
1  a
2  b
3  c
4  d
5  e
6  f
*/

krs,
tesu

Alright, i cant get this to work. Im not sure what u mean by substitute argv[1], argv[2], ... for the hard-coded names in the open statements.

Sorry, but i am very new to c++, only been using it for a few weeks now so i will need more precise instructions if you could pls.

Here is what I meant

int main(int argc, char* argv[]) // declaration of all variables
{
    if( argc != 4)
    {
           cout << "Now enough arguments\n";
           return 1;
    }
	string line;
	fstream chapter_one (argv[1]); //open the 3 text files
	fstream chapter_two (argv[2]);
	fstream chapter_three (argv[3]);
	ofstream book (argv[4]); //creates book.txt

ah ok i see how now, i was replacing the file names instead of the variables. Ill try this out and if i can't find a solution ill post back

Im still having problems with this. Can any1 help me please?

Let me have a look in my crystal ball...

Post your code, and tell us which part works, and which part doesn't etc..

Member Avatar for iamthwee

beware of EOF

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.