如何管理C++的二进制文件存储

发布于 2025-01-21 12:20:48 字数 1325 浏览 2 评论 0原文

我需要正确管理用.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文