Thread: File Copying

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    4

    File Copying

    Hi i am having a problem copying data from 1 file to another heres is my code.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    void copyfiles(ifstream& copyfrom, ofstream& copyto);
    int main()
    {
    ofstream outfile;
    ifstream infile;
    
    infile.open("dates.txt");
    if (infile.fail())
    {
        cout << "cudnt open" <<endl;
            exit (1);
    }
    
    outfile.open("results.txt");
    if (outfile.fail())
    {
        cout << "cudnt open" <<endl;
            exit (1);
    }
    
    copyfiles(infile, outfile);
    cout<<"done"<<endl;
    
    infile.close();
    outfile.close();
    
    return 0;
    }
    
    void copyfiles(ifstream& copyfrom, ofstream& copyto)
    {
        int date;
        date = copyfrom.get();
        while ( ! copyfrom.eof())
        {
            copyto.put(date);
            date = copyfrom.get();
        }
        return;
    }
    I am supposed to be using a class to pass the data across and manipulate it on the way, but i need to understand how to do this first. It compiles fine, then when i run the program nothing happens. 'Result.txt' isnt even created. Cheers for your help.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    First, define "nothing happens"... do you see any output at all?

    The eof test should not be used to control the loop these things are problematic when used in such a way. Several versions of the get function return a reference to the stream/file that can be tested directly to determine whether or not a value was read from said stream/file. I would suggest something more like this for the copyfiles function:

    Code:
    void copyfiles(ifstream& copyfrom, ofstream& copyto)
    {
        char date;
    
        while( !copyfrom.get(date) )
        {
            copyto.put(date);
        }
    
        return;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    http://msdn.microsoft.com/library/de...s/copyfile.asp

    unless you absolutely need to bug yourself with a lot of issues (and you are coding for win32) one line of code is perfectly enough for you. I use API as much as i can for
    1. less work to be done.
    2. Already tested and documented functions.

  4. #4
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> It compiles fine, then when i run the program nothing happens. 'Result.txt' isnt even created.

    i tested it and it works as-is.. copies the file to results.txt..

    currently im on devc++ and winxp..

    thought youd like to know

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    hk_mp5kpdw, i believe your solution should be
    Code:
    while (copyfrom.get(date))
    and you have a ! before the function call
    "What are all you parallelograms doing here?" - Peter Griffin (to Joe and his wheelchair buddies)

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, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03: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, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04: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