Copying a File

This is a discussion on Copying a File within the C++ Programming forums, part of the General Programming Boards category; Does anyone know if there is a function in the fstream.h library that will allow me to copy the contents ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    Copying a File

    Does anyone know if there is a function in the fstream.h library that will allow me to copy the contents of one file to another one with just a different file name?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    No, but it's fairly simple to do.

    -open both files
    -if both opened successfully:
    loop:
    -read a byte from source file
    -if not EOF:
    -write the byte to destination file
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Here is the code I was trying to use to copy the file.

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    
    int main()
    {
    	ifstream InFile("source.txt", ios::nocreate);
    	if (InFile.fail()){
    		cout << "Source.txt could not be opened";
    	}
    	ifstream InFile("src.txt", ios::nocreate);
    	if (InFile.fail()){
    		InFile("src.txt", ios::create);
    		cout << "src.txt was not found and has been created";
    	}
    	return(0);
    
    	else {
    		char Character;
    		while (InFile.get(Character)){
    			if (inFile.eof()){
    				cout << "File Copied" << endl;
    			}
    			else{
    				ofstream OutFile("src.txt");
    				cout << "Copying File contents" << endl;
    			}
    		}
    	}
    	return (0);
    }
    I can't get it to work.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    A lot of logical errors in your code...I'm surprised it even compiled, what with the return statement between the first if/else.

    The next most glaring problem is you are not copying the byte to the new file.


    Here's a sample C function:


    Code:
    /*
    *   return codes:
    *   -1   (Source file not found)
    *   -2   (Dest file couldn't be opened)
    *   >= 0 (Number of bytes copied)
    */
    int
     copy_file(const char destination[], const char source[])
    {
      int byte, result = 0;
      
      FILE * in, * out;
    
      in = fopen(source, "r");
      
          if(in == NULL)
           result = -1;
    
          else
         {
          out = fopen(destination, "w");
         
              if(out == NULL)
               result = -2;
             
              else
             {
                  while(EOF != (byte = fgetc(in)))
                 {
                  fputc(byte, out);
    
                  ++result;
                 }
                 
              fclose(out);
             }
    
          fclose(in);
         }
        
     return result;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    I made some changes to correct some of the errors I was getting during the compile. I read the code that was posted but I still can't figure out what I am doing wrong. Here is the new code
    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    
    int main()
    {
    	ifstream InFile("source.txt", ios::nocreate);
    	if (InFile.fail()){
    		cout << "Source.txt could not be opened";
    	}
    	ifstream InFile("src.txt", ios::nocreate);
    	if (InFile.fail()){
    		InFile("src.txt");
    		cout << "src.txt was not found and has been created";
    	}
    	else {
    		char Character;
    		while (InFile.get(Character)){
    			if (InFile.eof()){
    				cout << "File Copied" << endl;
    			}
    			else{
    				ofstream OutFile("src.txt");
    				cout << "Copying File contents" << endl;
    			}
    		}
    	}
    	return (0);
    }

  6. #6
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    You may want to try something like this :

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    ifstream InFile;
    ofstream OutFile;
    
    char one;
    
    int main()
    {
        InFile.open("source.txt", ios::in | ios::nocreate);
        if(InFile)
        {
            OutFile.open("write.txt", ios::out);
            while(InFile.get(one))
            {
                while(one != EOF)
                {
                    OutFile << one;
                }
            }
    InFile.close();
    OutFile.close();
        }
    }

    I've made a LOT of mistakes here, but I've just come back from a party However, I'm sure you get the point : you will need to open the output file using ofstream and not ifstream. You also may need to open it using ios::binary, I'm not sure. Otherwise, I think that's pretty much it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. 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
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM

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