copy multiple files to a destination file

This is a discussion on copy multiple files to a destination file within the C++ Programming forums, part of the General Programming Boards category; I have to write a program that emulates the Unix cat command. The program accepts between 2 to 9 files ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    22

    copy multiple files to a destination file

    I have to write a program that emulates the Unix cat command. The program accepts between 2 to 9 files from dos command line.
    eg concat file 1 file 2 file3 ...... // concat being the name of my program....files 2 and 3 will be copied to file 1 in that order..... I have written a program that allows for 3 arguments.... copying the second into the first...how would i go about changing it to allow for multiply files..........this is the code I have

    Code:
    #include <fstream.h>	//for I/O files
    #include <iostream.h>
    using namespace std;
    #include <process.h>		//for exit
    
    int main(int argc, char* argv[])
    {
    	if(argc != 3)
    	{
    		cerr << "\nFormat: ocopy srcfile destfile";
    		exit(-1);
    	}
    
    	char ch;
    	
    	ifstream infile;		//create input file
    	infile.open(argv[1]);	//open file
    
    	if(!infile)
    	{cerr << "\nCan't open " << argv[1]; exit(-1);}
    
    	ofstream outfile;		//create output file
    	outfile.open(argv[2]);	//open output file
    
    	if(!outfile)
    	{cerr << "\nCan't open " << argv[2]; exit(-1);}
    
    	while(infile)			//until EOF
    	{
    		infile.get(ch);		//read a character
    		outfile.put(ch);	//write a character
    	}
    
    	return 0;
    }

    any help would be great.....thanks

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Open the output file, then start a loop going through the remaining command line args, opening each file and writing it's contents to the first file.

    Some ideas on command line args:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Thanks...thats what I thought...now just to give it a try...

    Thanks also for the links....I'm sure they will help.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-22-2009, 11:30 PM
  2. Input/Output Files
    By zrsmith2 in forum C Programming
    Replies: 3
    Last Post: 06-15-2008, 10:30 PM
  3. single File Pointer to Multiple Files
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 12:49 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21