如何管理C++的二进制文件存储
我需要正确管理用.zip
扩展名来复制和粘贴压缩文件的二进制文件流。 我实际上是在尝试制作一个简单的测试项目:我想复制./ src/directory.zip
in ./ dest
目录最初是空的。 代码编译正确完成,但是在程序结束时,目标目录仍然为空,我不知道出于哪个原因。 这是代码:
#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;
}
我知道这可以简单地使用std :: filesystem :: copy_file
或其他高级函数,但是这些二进制文件将通过服务器从服务器从服务器发送到客户端。首先,我想在本地目录中让这项工作。
我刚刚遵循答案中的建议,我得到了这个错误: 没有此类文件或目录
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论