'How to manage binary files storage in C++
I need to correctly manage binary files streams for copying and pasting compressed files with .zip extension.
I'm actually trying to make a simple test project: I would like to copy ./src/Directory.zip in ./dest directory initially empty.
The code compiling is correctly done, but at the end of the program the destination directory is still empty and I don't know for which reason.
This is the code:
#include <iostream>
#include <fstream>
#include <vector>
typedef unsigned char BYTE;
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<BYTE> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
int main(){
std::vector<BYTE> fileData = readFile("./src/Directory.zip");
std::ofstream destFile;
destFile = std::ofstream("./dest/Directory.zip", std::ios::out | std::ios::binary);
if ( !destFile )
std::cout << std::strerror(errno) << '\n';
destFile.write( (char*) &fileData[0], sizeof(BYTE)*fileData.size() );
destFile.close();
return 0;
}
I know this could be simply done with std::filesystem::copy_file or other high-level functions, but these binary files will be sent through a socket from a server to a client. Firstly I would like to let this work in a local directory.
I've just followed the advice in the answer and I get this error:
No such file or directory
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
