copy files in c++

Discussion in 'C++' started by varun.chinta@gmail.com, Jun 1, 2006.

  1. Guest

    Hi,
    I am doing a project which involves basic file operations like file
    open, file copy, file rename, file remove. can anyone suggest me how to
    do a file copy. The file that i create using the file open function, i
    would like to copy that same file to another location.
    should i use pointer or is there a better way to do it.
    Thanks
    , Jun 1, 2006
    #1
    1. Advertising

  2. Mike Wahler Guest

    <> wrote in message
    news:...
    > Hi,
    > I am doing a project which involves basic file operations like file
    > open, file copy, file rename, file remove.
    > can anyone suggest me how to
    > do a file copy. The file that i create using the file open function, i
    > would like to copy that same file to another location.
    > should i use pointer or is there a better way to do it.


    #include <algorithm>
    #include <fstream>
    #include <ios>
    #include <iostream>
    #include <iterator>
    #include <string>

    int main()
    {

    const std::string source_loc("c:/");
    const std::string dest_loc("c:/xyz/");
    const std::string filename("file1");

    std::ifstream in((source_loc + filename).c_str());
    std::eek:fstream out((dest_loc + filename).c_str());

    if(in && out)
    {
    in >> std::noskipws;

    std::copy(std::istream_iterator<char>(in),
    std::istream_iterator<char>(),
    std::eek:stream_iterator<char>(out));

    }

    if(!in.eof())
    std::cerr << "Error occurred reading input\n";

    if(!out)
    std::cerr << "Error occurred writing output\n";

    return 0;
    }

    Note: the values of the strings 'source_loc' and 'dest_loc'
    are subject to platform restrictions for file names. (I ran
    this on Microsoft Windows).

    -Mike
    Mike Wahler, Jun 1, 2006
    #2
    1. Advertising

  3. Mike Wahler wrote:
    >
    >
    > #include <algorithm>
    > #include <fstream>
    > #include <ios>
    > #include <iostream>
    > #include <iterator>
    > #include <string>
    >
    > int main()
    > {
    >
    > const std::string source_loc("c:/");
    > const std::string dest_loc("c:/xyz/");
    > const std::string filename("file1");
    >
    > std::ifstream in((source_loc + filename).c_str());
    > std::eek:fstream out((dest_loc + filename).c_str());
    >
    > if(in && out)
    > {
    > in >> std::noskipws;
    >
    > std::copy(std::istream_iterator<char>(in),
    > std::istream_iterator<char>(),
    > std::eek:stream_iterator<char>(out));
    >
    > }
    >
    > if(!in.eof())
    > std::cerr << "Error occurred reading input\n";
    >
    > if(!out)
    > std::cerr << "Error occurred writing output\n";
    >
    > return 0;
    > }
    >
    > Note: the values of the strings 'source_loc' and 'dest_loc'
    > are subject to platform restrictions for file names. (I ran
    > this on Microsoft Windows).
    >
    > -Mike
    >
    >


    Hi,

    Replacing the code within the if(in && out) {...} block, I often do

    if(in && out)
    {
    out<<in.rdbuf();
    }

    Is there any reason why the std::copy method above is to be preferred?


    Sincerely,

    Peter Jansson
    http://www.p-jansson.com/
    http://www.jansson.net/
    Peter Jansson, Jun 1, 2006
    #3
  4. Guest

    Peter Jansson wrote:
    > Mike Wahler wrote:
    > >
    > >
    > > #include <algorithm>
    > > #include <fstream>
    > > #include <ios>
    > > #include <iostream>
    > > #include <iterator>
    > > #include <string>
    > >
    > > int main()
    > > {
    > >
    > > const std::string source_loc("c:/");
    > > const std::string dest_loc("c:/xyz/");
    > > const std::string filename("file1");
    > >
    > > std::ifstream in((source_loc + filename).c_str());
    > > std::eek:fstream out((dest_loc + filename).c_str());
    > >
    > > if(in && out)
    > > {
    > > in >> std::noskipws;
    > >
    > > std::copy(std::istream_iterator<char>(in),
    > > std::istream_iterator<char>(),
    > > std::eek:stream_iterator<char>(out));
    > >
    > > }
    > >
    > > if(!in.eof())
    > > std::cerr << "Error occurred reading input\n";
    > >
    > > if(!out)
    > > std::cerr << "Error occurred writing output\n";
    > >
    > > return 0;
    > > }
    > >
    > > Note: the values of the strings 'source_loc' and 'dest_loc'
    > > are subject to platform restrictions for file names. (I ran
    > > this on Microsoft Windows).
    > >
    > > -Mike
    > >
    > >

    >
    > Hi,
    >
    > Replacing the code within the if(in && out) {...} block, I often do
    >
    > if(in && out)
    > {
    > out<<in.rdbuf();
    > }
    >
    > Is there any reason why the std::copy method above is to be preferred?
    >
    >
    > Sincerely,
    >
    > Peter Jansson
    > http://www.p-jansson.com/
    > http://www.jansson.net/


    Thank you for the suggestions, i tried using the below method and it
    worked.
    ifstream Source("C:/abc.txt");
    ofstream Dest("C:/abc1.txt");
    Dest << Source.rdbuf();
    , Jun 1, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Blair
    Replies:
    4
    Views:
    1,076
    John Saunders
    Jan 12, 2005
  2. Steve Richter
    Replies:
    4
    Views:
    5,439
    Steve Richter
    Apr 18, 2005
  3. Alex
    Replies:
    2
    Views:
    1,202
  4. Replies:
    26
    Views:
    2,091
    Roland Pibinger
    Sep 1, 2006
  5. Tim Golden
    Replies:
    0
    Views:
    672
    Tim Golden
    Apr 19, 2010
Loading...

Share This Page