-4

I opened two files and I want to find the most efficient way to put data from out file to in fill. I tried rdbuf() but it doesn't work and the outfile is the same so I need to have more explanation for rdbuf() and is there another method to append a file to another?

ofstream writeto;
writeto.open(srr.c_str(),ios::app);
cout<<"give me the file you want to copy from  : ";
string written;
cin>>written;
ifstream writefrom;
writefrom.open(written.c_str());
bcr
  • 950
  • 8
  • 28

2 Answers2

2

Assuming you want to copy the entire input file:

writeto << writefrom.rdbuf();

Also, you should use std::getline() instead of operator>> so you can account for file path/names that have space characters in them:

getline(cin, written);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • i tried writefrom.rdbuf() but the file don't change so i asked here to get another answer or to know why this don't work – Ahmed Hanafy El Drawy Apr 05 '18 at 22:43
  • `std::ofstream` has an `operator<<` that takes a `std::basic_streambuf*` as input. `std::ifstream::rdbuf()` returns a `std::basic_filebuf*`, and `std::basic_filebuf` derives from `std::basic_streambuf`, so it should work. Did you verify that `writeto.open()` and `writefrom.open()` are both successful? There is no error handling in the code you showed. – Remy Lebeau Apr 05 '18 at 23:24
-2

Read a file into a stream object, read the next file into the stream. Write out the stream.

Mike
  • 190
  • 7