c ++读取字符串变量中包含的二进制数据

时间:2016-01-26 22:26:16

标签: c++ string binary

我有一个函数,它使用zlib将zip文件中的文件解压缩到内存,然后将生成的文件内容存储在字符串对象中。问题是字符串现在由文件的二进制表示组成,我不知道如何在块中读取该字符串,如

ifstream::read():
file.read((char*)result, sizeof(int));

我必须逐块读取数据,但是我没有找到任何从字符串变量中执行此操作的过程。

这是将文件内容作为字符串返回的函数(如果有更好的方法,我可以更改函数):

string externalresourcemanager::getFileInsideZip(string zipFile, string fileInZip) {
    int err = UNZ_OK;                 // error status
    uInt size_buf = WRITEBUFFERSIZE;  // byte size of buffer to store raw csv data
    void* buf;                        // the buffer
    string sout;                      // output strings
    char filename_inzip[256];         // for unzGetCurrentFileInfo
    unz_file_info file_info;          // for unzGetCurrentFileInfo

    unzFile uf = unzOpen(zipFile.c_str()); // open zipfile stream
    if (uf==NULL) {
        cerr << "Cannot open " << zipFile << endl;
        return sout;
    } // file is open

    if ( unzLocateFile(uf,fileInZip.c_str(),1) ) { // try to locate file inside zip
        // second argument of unzLocateFile: 1 = case sensitive, 0 = case-insensitive
        cerr << "File " << fileInZip << " not found in " << zipFile << endl;
        return sout;
    } // file inside zip found

    if (unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0)) {
        cerr << "Error " << err << " with zipfile " << zipFile << " in unzGetCurrentFileInfo." << endl;
        return sout;
    } // obtained the necessary details about file inside zip

    buf = (void*)malloc(size_buf); // setup buffer
    if (buf==NULL) {
        cerr << "Error allocating memory for read buffer" << endl;
        return sout;
    } // buffer ready

    err = unzOpenCurrentFilePassword(uf,NULL); // Open the file inside the zip (password = NULL)
    if (err!=UNZ_OK) {
        cerr << "Error " << err << " with zipfile " << zipFile << " in unzOpenCurrentFilePassword." << endl;
        return sout;
    } // file inside the zip is open

    // Copy contents of the file inside the zip to the buffer
    cout << "Extracting: " << filename_inzip << " from " << zipFile << endl;
    do {
        err = unzReadCurrentFile(uf,buf,size_buf);
        if (err<0) {
            cerr << "Error " << err << " with zipfile " << zipFile << " in unzReadCurrentFile" << endl;
            sout = ""; // empty output string
            break;
        }
        // copy the buffer to a string
        if (err>0) for (int i = 0; i < (int) err; i++) sout.push_back( *(((char*)buf)+i) );
    } while (err>0);

    err = unzCloseCurrentFile (uf);  // close the zipfile
    if (err!=UNZ_OK) {
            cerr << "Error " << err << " with zipfile " << zipFile << " in unzCloseCurrentFile" << endl;
            sout = ""; // empty output string
        }

    free(buf); // free up buffer memory
    return sout;
}

谢谢!

编辑:

我尝试你的解决方案(约翰),但我得到一个空的结果。以下是解释问题的示例代码:

   int store = 1024;
    string result = "";

    ofstream file("test.txt", std::ios::binary);
    //save a few data
    file.write(reinterpret_cast<const char*>(&store), sizeof(int));
    file.write(reinterpret_cast<const char*>(&store), sizeof(int));
    file.close();

    //remember that i receive the "string binary data" from the getFileInsideZip() function (here i simulating the problem and store the binary data in a string only for explanation purpose)
    ifstream newFile("test.txt", std::ios::binary);

    newFile.seekg(0, std::ios::end);
    int length = newFile.tellg();
    newFile.seekg(0, std::ios::beg);

    char* begin = &*result.begin();
    //here i have a binary file in a string variable
    newFile.read(begin, length);
    newFile.close();

    cout << "result:" << result << "\n";

    //now im trying to get the first int from this binary data
    int stored = 0;
    std::istringstream bin(result);
    bin.read((char*)&stored, sizeof(int));

    cout << "stored: " << stored << "\n";

    //but the output is:
    //result -> empty (initial value)
    //stored: 0 -> (initial value)

any suggestion?

0 个答案:

没有答案
相关问题