1

I need to duplicate a file that I generate using "ofstream". I wanted to know if there is any way without having to do it manually (creating another stream, reading the one I want to copy and writing what it was read) I search for this question in google, but all I could find was something that work in Windows, and I am working with linux

I am kind of new to c++ :)

xuandl
  • 183
  • 1
  • 2
  • 14
  • 2
    I wish `cp` was written in C++... – Sean Bright Jan 17 '13 at 21:16
  • 2
    `std::ifstream src("from.txt"); std::ofstream dst("to.txt"); dst << src.rdbuf();` see this post: [link](http://stackoverflow.com/questions/10195343/copy-a-file-in-an-sane-safe-and-efficient-way) – Balázs Édes Jan 17 '13 at 21:18
  • "Duplicating a file" is *very* vague and depending on what you mean by it, it can be **impossible** to get right without platform-dependent code. Do you want to keep the same timestamps? What about the file attributes, alternate streams, etc.? What do you want to do about compressed or sparse files? What about the file permissions? ... – user541686 Jan 17 '13 at 21:20

1 Answers1

1

Sounds like a job for boost::iostreams::tee.

NPE
  • 486,780
  • 108
  • 951
  • 1,012