从Cocos2dx的指针char向量写入sqlite文件

Write to sqlite file from vector of char of pointer in Cocos2dx

本文关键字:sqlite 文件 向量 char Cocos2dx 指针      更新时间:2023-10-16

我正在使用HttpClient类。当我得到响应时,我想要将响应数据写入文件。当我检查文件时,它的格式错误,不能作为正常的sqlite文件打开。我的代码有什么问题?

 // Get data
    std::string path = cocos2d::FileUtils::getInstance()->getWritablePath()
            + "test.db3";
    auto buffer = response->getResponseData();
    FILE *pFile;
    pFile = fopen(path.c_str(), "wb");
    int size = buffer->size();
    fwrite(&size, sizeof(int), 1, pFile);
    fclose(pFile);

OP.

// Get data
    std::string path = cocos2d::FileUtils::getInstance()->getWritablePath()
            + "serverdb.sqlite";
    auto buffer = response->getResponseData();
    std::ofstream ofs;
    ofs.open(path.c_str(), std::ios::out | std::ios::trunc);
    ofs.write(&(buffer->front()), buffer->size());
    ofs.flush();
    ofs.close();