C++ c++;读取字符串变量中包含的二进制数据
我有一个函数,它使用zlib将文件从zip文件解压到内存,然后将生成的文件内容存储在字符串对象中。问题是字符串现在由文件的二进制表示形式组成,我不知道如何在块中读取该字符串,如C++ c++;读取字符串变量中包含的二进制数据,c++,string,binary,C++,String,Binary,我有一个函数,它使用zlib将文件从zip文件解压到内存,然后将生成的文件内容存储在字符串对象中。问题是字符串现在由文件的二进制表示形式组成,我不知道如何在块中读取该字符串,如 ifstream::read(): file.read((char*)result, sizeof(int)); 有必要逐块读取数据,但我还没有找到任何从字符串变量读取数据的过程 这是一个以字符串形式返回文件内容的函数(如果有更好的方法,我可以更改该函数): string externalresourcemanager
ifstream::read():
file.read((char*)result, sizeof(int));
有必要逐块读取数据,但我还没有找到任何从字符串变量读取数据的过程
这是一个以字符串形式返回文件内容的函数(如果有更好的方法,我可以更改该函数):
string externalresourcemanager::getFileInsideZip(string zipFile,string fileInZip){
int err=UNZ_OK;//错误状态
uInt size\u buf=WRITEBUFFERSIZE;//用于存储原始csv数据的缓冲区字节大小
void*buf;//缓冲区
字符串sout;//输出字符串
char filename_inzip[256];//用于unzGetCurrentFileInfo
unz_file_info file_info;//用于unzGetCurrentFileInfo
unzFile uf=unzOpen(zipFile.c_str());//打开zipFile流
如果(uf==NULL){
cerr很难理解问题是什么。你说你不知道如何以块的形式读取文件,但这正是unzReadCurrentFile(uf,buf,size_buf);
所做的。你想用ifstream::read()读取zip文件吗:
?如果你想这样做,你必须自己解压数据。这没有多大意义,所以我正在努力理解你到底想要什么。好吧,也许我明白了。所有这些zip文件内容都是无关的。你真正想做的是对字符串进行二进制读取,查找std::istringstream
。做你需要的一切。我编辑我在帖子中添加了你的建议,但问题仍然存在。
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?