C++ Program to copy Image files using C++ File handling – Non Text Files

C++ program to copy image files using C++ file handling. The first temptation was to open non Text File in binary mode and read one byte at a time from the source file and write the same at the destination.

BUT this is not working with Image file and others as something is missing in this case and we are not able to open our duplicate file in any image editor.  Here is the simple code that read the whole image in memory buffer and write the same in our destination file.

Source code of Image copy program

//program to make copy of any image file 
// made by    : rakesh kumar

#include
#include
using namespace std;
int main()
{
	//ofstream fout("copy.jpg");
	//ifstream fin("Image-1.jpg",ios::binary);
	//char ch;
	//while(fin.read((char *)&ch,sizeof(ch)))
	//		fout.write((char *)&ch,sizeof(ch));
	std::ofstream("copy.jpg", std::ios::binary) << std::ifstream("image-1.jpg", std::ios::binary).rdbuf();
	return 0;
}

Here in the above program “image-1.jpg” is our source file and “copy-1.jpg” is the duplicate file we just created using this program.

If you have any other idea to perform the same using C++, please let us know.

%d bloggers like this: