Add not overwrite to a file

This is a discussion on Add not overwrite to a 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

    Add not overwrite to a 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 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...

  2. #2
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Why not just use the append opening mode?

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    Not sure how to use append???

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try closing infile after reading each file.
    Code:
            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.get(ch))			//read until EOF
    	        {
    		        outfile.put(ch);	//write a character
    	        }
    		infile.close();
            }
    Last edited by swoopy; 10-02-2003 at 01:37 PM.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    22
    thanks...but that doesn't work.....

  6. #6
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    On how to use append, take a look at your library's references.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Bones
    Not sure how to use append???
    Hint
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

    Cheers

    Thanks ....I managed to get my program working with append....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 08:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM

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