gpt4 book ai didi

C++ 打开一个二进制文件

转载 作者:行者123 更新时间:2023-11-28 02:58:47 25 4
gpt4 key购买 nike

我想用 C++ 打开一个二进制文件。但我在 C 中有这个功能:

uint openPacket(const char* filename, unsigned char** buffer) {
FILE* fp = fopen(filename, "rb");
if (fp) {
size_t result;
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
rewind(fp);
*buffer = new unsigned char[fsize];
result = fread(*buffer, 1, fsize, fp);
if (result != fsize) {
printf("Reading error in %s, unable to send packet!\n", filename);
delete[] * buffer;
fsize = 0;
}
fclose(fp);
return fsize;
} else {
printf("Couldn't open %s for packet reading, unable to send packet!\n", filename);
return 0;
}
}

我想制作类似的东西:string OpenPacket(string filename)但不工作:(

最佳答案

可能这是一个可能的包装函数:

std::string openPacket( const std::string& filename )
{
unsigned char* buff;
uint size = openPacket( filename.c_str(), &buff );
if( size )
{
std::string s( reinterpret_cast<const char*>(buff), size );
delete [] buff;
return s;
}
return std::string();
}

关于C++ 打开一个二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21403583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com