Can someone help me to start this assignment. i am totally lost on what needs to be done .. can someone provide me with some input ... would be great help .... not asking for someone to write me the code .. just tell me what needs to be done

Write a program that concatenates the contents of several files into one file. For example,
catfiles chapter1.txt chapter2.txt chapter3.txt book.txt
makes a long file book.txt that contains the contents of the files chapter1.txt, chapter2.txt, and
chapter3.txt. The target file is always the last file specified on the command line.

9 Years
Discussion Span
Last Post by vijayan121

Since you didn't give much spec aside from the description, here's some rough pseudo-code:

int main(int argc, char** argv) {
  // should check for the right number of arguments before going past this point
  ofstream outfile(argv[argc - 1]); // last argument
  for(int i = 1; i < argc - 1; i++) { // loop over small files
    ifstream infile(argv[i]); // small file
    read from infile and write to outfile, probably in another loop
  return 0;

Obviously, I left a lot out, but hopefully you get the idea :)

open output file

Use a loop to
    open input file
    read input file
    write to output file
    close input file
end of loop
close output file

anywys i dun think i got this one ... too lost with this topic .. handed in my stuff without this one ... and my stupid ta does not give out the correct codes even after the due date ... so i am wondering if somoene can do this for me and post it if they don't mind .. as i am way too lost and want to c how could this be done

#include <fstream>
#include <iterator>
#include <algorithm>
using namespace std;

struct copy_file_to_stream_t 
  explicit copy_file_to_stream_t( ostream& s ) : stm(s) {}
  void operator()( char* file_name ) const
    ifstream file(file_name) ; file >> noskipws ;
    istream_iterator<char> begin(file), end ;
    copy( begin, end, ostream_iterator<char>(stm) ) ;
  ostream& stm ;

int main( int argc, char** argv )
  if( argc > 2 )
    ofstream file( argv[ argc-1 ] ) ;
    copy_file_to_stream_t copy_file_to_stream(file) ;
    for_each( argv+1, argv+argc-1, copy_file_to_stream ) ;

*** warning *** do not submit this; you will get into trouble if you do.


well this is how mch i got after doing some more reading

int main(int argc, char* argv[])
   if (argc != 5)
      cout << "Usage: ExP10_14 sourcefile targetfile";
      return 1;

   ofstream final;
 for(int i = 1; i < argc - 1; i++) { 
    ifstream infile;
    bool more = true;
   while (more)
      char ch;
      if (infile.fail()) more = false;
      else final.put(ch);
   return 0;

can someone tell me if its correct or not .....

thanks vijay but i think i am still lost on your code .... see if you can help me with mine .... can someone help me on how can i read the input from the command promt i mean the filename and seperate them into 5 argv


If argc equals 5 then the last valid index for argv is 4, not 5, since array indexing is zero based. That also means that argc - 1 should probably be argc - 2.

Typically you would just declare the ifstream once by declaring it outside of the for loop. Ditto for the variable ch, declare it outside both the for and the while loop.

The while loop syntax could be simplified to this:

while (infile >> ch)
    final << ch;

Now when infile fails it will stop the while loop. It could fail for two reasons. First it could fail because it found EOF meaning the current file was successfully read in it's entirety. Alternatively, it could fail for some other reason. You can test for the former by calling eof(). Don't fall into the trap of using the return value of eof() as the while loop conditional.

In either case, once the infile has failed, you may need to clear it with clear() before you close it with close() before you reuse it.


can someone tell me how to check this dam thing lol .... i mean how do i use the command promt argument thingy .... i have no clue on how to run this thing


can someone tell me how to check this dam thing lol .... i mean how do i use the command promt argument thingy .... i have no clue on how to run this thing

<your program name here> file1.txt file2.txt file3.txt result.txt

this line is ok as it is:
for(int i = 1; i < argc - 1; i++)


do i run it from dos ??? or visual basic .... its stupid question but would be gr8 if someone can tell me .. i use the compile method but gives out the error message ...


run it from the command line. if you are on windows, either open a command window or
type in the command line in the Run.. dialog opened from the windows menu.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.