I have written code to read a big size file content and write that content into a new file.
That code works fine with a small and medium size file content but with a big size file content, approximately 1.8GB and above it does not works and gives me an unknown error/exception during runtime.
Also, I have tried to debug and the following is the debugging result:
The code:
char * myClass::getFileContent(const char * fileName) {
std::ifstream file(fileName, std::ios::binary|std::ios::ate);
if (!file.is_open()) {
perror(strerror(errno));
return "";
}
char * strBuffer = NULL;
long long length = file.tellg();
strBuffer = new char[length];
file.seekg(0, std::ios::beg);
file.read(strBuffer, length);
return strBuffer;
}
// The implementation
char *fileName = "C:\\desktop-amd64.iso";
char *fileContent = myClass.getFileContent(fileName);
ofstream file("c:\\file.iso", ios::binary);
if (file.is_open()) {
file.write(fileContent, myClass.getFileSize(fileName));
file.close();
}
delete fileContent;
Note: I am using Visual Studio 2015 on Windows 7 x64.
Why does that problem happen with the big files?