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 can't seem to get my program to add file 2 and 3 to file 1....it always overwrites file 1....this is the code I have...
Code:
int main(int argc, char* argv[])
{
if((argc < 2 ) || (argc > 9))
{
cerr << "\nFormat: ocopy srcfile destfile";
exit(1);
}
char ch;
ofstream outfile; //create output file
outfile.open(argv[1]); //open output file
if(!outfile)
{cerr << "\nCan't open " << argv[1]; exit(1);}
for (int i = 1; i < argc; i++)
{
ifstream infile; //create input file
infile.open(argv[i]); //open file
if(!infile)
{cerr << "\nCan't open " << argv[i]; exit(1);}
while(infile) //until EOF
{
infile.get(ch); //read a character
outfile.put(ch); //write a character
}
}
return 0;
}
Any suggestions would be great...thanks...