   Unable to copy a image file in C++
  Hi, i want to divide image file into packets. i did this in text file it worked fine but its not working in PNG file. Why its not working? i am unable to create copy of the image. Whats wrong with this code!CODE :#include <iostream>#include <fstream>using namespace std;int main() {f1.open("D:\\xx.png",ios::binary);if(!f1.is_open()) {cout<<"Unable to open file\n";return 0;}f1.seekg(0,ios::end);int length = f1.tellg();f1.seekg(0,ios::beg);fstream f2("xxx.png",ios:out|ios::binary);const int nBytes = 30720;char *buffer = new char [nBytes + 1];int nRead(0);const int bytes = 30720;char buffer[bytes + 1];while(!f1.eof()) {nRead = f1.read(buffer,bytes).tellg ();if (nRead <= -1) {int temp = length % bytes;buffer[temp] = 0;f2<<buffer;}else {buffer[bytes] = 0;f2<<buffer;}}f1.close();f2.close();}   
  You're writing unformatted data to the file using formatted I/O functions. Use write() to write the data instead, just like you use read() to read it.   