读取文件到stringstream, 利用rdbuf一次性全部读取(包括0),如果使用 ifstream >> 直接读取到string会被0分割,无法一次性读取。
std::ifstream fs(file, std::ios::in | std::ios::binary);
if (!fs.is_open()) {
LOG(FATAL) << "Renderer could not load file=" << file;
}
std::stringstream ss;
ss << fs.rdbuf();
LOG(INFO) << "file=" << file << " ss.str().length()=" << ss.str().length();
string
std::string mask = ss.str();
cv::Mat
cv::Mat mask(cv::Size(540, 960), CV_8UC1, (void*)ss.str().data());
二进制文件按字节读到vector<unsigned char>
// Effective STL item 29:
ifstream inputFile("interestingData.txt");
vector<unsigned char> fileData((istreambuf_iterator<char>(inputFile)),
istreambuf_iterator<char>());
//作者:陈硕
//链接:https://www.zhihu.com/question/52359180/answer/130159131
//来源:知乎
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
转一个集合
#include <fstream>
#include <vector>
#include <cassert>
#include <boost/progress.hpp>
static size_t getFileSize(FILE* file) {
fseek(file, 0, SEEK_END);
size_t read_len = ftell(file);
fseek(file, 0, SEEK_SET);
return read_len;
}
static size_t getFileSize(const char* filePath) {
FILE* file = fopen(filePath, "rb");
if (file == nullptr) {
return 0;
}
size_t size = getFileSize(file);
fclose(file);
return size;
}
// 方法 1
std::vector<unsigned char> readFromFile1(const char* filePath) {
FILE* file = fopen(filePath, "rb");
std::vector<unsigned char> result;
if (file == nullptr) {
return result;
}
// 获取文件大小,尽量一次读完
size_t fileSize = getFileSize(file);
if (fileSize != 0) {
result.resize(fileSize);
size_t n = fread(&result[0], 1, fileSize, file);
assert(n <= fileSize);
if (n != fileSize) {
result.resize(n);
}
}
// 在读取过程当中,有可能文件大小有变化,再尝试读取
const size_t read_len = 1024;
char buf[read_len];
for (;;) {
size_t n = fread(buf, 1, read_len, file);
result.insert(result.end(), buf, buf + n);
if (n < read_len) {
break;
}
}
fclose(file);
return result;
}
// 方法 2
std::vector<unsigned char> readFromFile2(const char* filePath) {
std::ifstream inputFile(filePath, std::ios::binary);
std::vector<unsigned char> fileData((std::istreambuf_iterator<char>(inputFile)),
std::istreambuf_iterator<char>());
return fileData;
}
// 方法 3,在方法 2 基础上调用 reserve
std::vector<unsigned char> readFromFile3(const char* filePath) {
std::vector<unsigned char> fileData;
fileData.reserve(getFileSize(filePath));
std::ifstream inputFile(filePath, std::ios::binary);
fileData.insert(fileData.end(),
std::istreambuf_iterator<char>(inputFile),
std::istreambuf_iterator<char>());
return fileData;
}
// 方法 4,思路同方法 1, 只是换了 ifstream 实现,没有考意外情况,只测量速度
std::vector<unsigned char> readFromFile4(const char* filePath) {
std::vector<unsigned char> fileData;
std::ifstream inputFile(filePath, std::ios::binary);
inputFile.seekg(0, std::ios::end);
fileData.resize(inputFile.tellg());
inputFile.seekg(0);
inputFile.read(reinterpret_cast<char*>(fileData.data()), fileData.size());
return fileData;
}
int main(int argc, const char* argv[]) {
const char* filePath = "/电影/[阳光电影www.ygdy8.com].使徒行者.BD.720p.国粤双语中字.mkv";
{
boost::progress_timer t;
readFromFile1(filePath);
}
{
boost::progress_timer t;
readFromFile2(filePath);
}
{
boost::progress_timer t;
readFromFile3(filePath);
}
{
boost::progress_timer t;
readFromFile4(filePath);
}
return 0;
}
//作者:黄兢成
//链接:https://www.zhihu.com/question/52359180/answer/130202972
//来源:知乎
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
网友评论