I'm working on a small project in C++ which is all about copying an image file. So far, I can't seem to prevent data loss from happening. All images copied experience up to 2% of data loss (corruption). Is there a flaw in my code or is it something about image codecs that I'm not aware of? Also, is there a more efficient way to do this? The code is down below.
Thanks in advance.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <vector>
#define FILE_SRC "IMAGE1.jpg" // name of file to be copied
#define FILE_DES "IMAGE2.jpg" // name of file clone
using namespace std;
void copy_file()
{
puts("Press Any Key to Begin."); if (cin.ignore()) { ; }
vector <unsigned char> fileData; // data from source file will be temporarily stored in vector
fstream targetFile;
unsigned char targetByte;
// START : copies data from source file and stores in vector
targetFile.open(FILE_SRC, ios::in | ios::binary);
while (targetFile >> targetByte)
{
fileData.push_back(targetByte);
}
targetFile.close();
// END : copies data from source file and stores in vector
puts("File Loaded.");
// START : checks if destination file already exists , if not , create new
targetFile.open(FILE_DES);
if (targetFile.fail())
{
targetFile.close();
targetFile.open(FILE_DES,ios::out);
}
targetFile.close();
// END : checks if destination file already exists , if not , create new
targetFile.open(FILE_DES, ios::app | ios::binary);
// START : writes data from vector to destination file
for (unsigned int i = 0; i < fileData.size(); i++)
{
targetFile << fileData.at(i);
}
targetFile.close();
// END : writes data from vector to destination file
puts("File Copied.");
}
int main()
{
copy_file();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire