在内存中迭代以读取值
iterating through memory to reading values
我有一个将文本文件读入内存的当前代码:
std::streampos fsize = 0;
std::ifstream file(fileName, std::ios::binary); // open file
if(!file.good()) {
std::cout << "Error opening file";
return 0;
}
// get length of file
file.seekg(0, ios::end);
fsize = file.tellg();
// allocate memory
char *memory = new char[fsize];
// read data as a block
file.seekg (0, ios::beg);
file.read (memory, fsize);
file.close(); // close file
return fsize;
现在我有代码对它进行迭代。如果这行以"v"开头,它将读取前面的3个浮点值,如果它以"n"开头,则读取相同的浮点值,但进入不同的数组。
char* p = memory; // pointer to start of memory
char* e = memory + fsize; // pointer to end of memory
while (p != e) {
if (memcmp(p, "v", 1) == 0) {
sscanf(p, "v %f %f %f", &a[vI], &b[vI], &c[vI]);
vI++;
} else if (memcmp(p, "n", 1) == 0) {
sscanf(p, "v %f %f %f", &d[nI], &e[nI], &f[nI]);
nI++;
while (*p++ != (char) 0x0A);
}
我知道一定有更好/更安全的方法来做这件事。
我假设您有一个文本文件。这可以做得简单得多。首先,不要以二进制模式打开文件,只需读取lineise即可。以下是一种可能的实现方式:
template<class output_iterator>
void read_file(std::istream &input, output_iterator v1, output_iterator v2,
output_iterator v1) {
std::string line_buffer;
while(std::getline(input, line_buffer)) { // read each line of text
if(line_buffer[0] == 'v') {
std::stringstream line_stream(line_buffer.substr(1)); // drop the 'v'
// read three consecutive floats
line_stream >> *v1++ >> *v2++ >> *v3++;
}
}
}
此代码假定以'v'
开头的行是良好的格式。你可以这样使用它:
std::vector<float> values1, values2, values3;
std::fstream input_file(fileName);
read_file(input_file, std::back_inserter(values1), std::back_inserter(values2),
std::back_inserter(values3));
如果您的系统支持mmap(即任何类似*nix的系统),请使用它。这将(非常快)为您提供文件内容的char*
。如果你的文件很大,这很好,因为它使用虚拟内存系统为你缓存所有内容——也就是说,你不必等待所有数据的复制。mmap函数立即返回,并且虚拟内存的相关部分已经映射到您的文件。
int fd = open(binaryRAWFileName, O_RDONLY);
... should do some error check to ensure fd != -1
// get the size of the file
struct stat sb;
if (fstat(fd, &sb) == -1) {
... there was an error with fstat
}
char * memory = mmap(NULL // we don't care where the memory is
, sb.st_size // length of the file
, PROT_READ
, MAP_PRIVATE
, fd // the file descriptor of course
, 0);
最后,长度为1的memcmp
似乎有点毫无意义。为什么不只使用if(*p=='v')
而不是if(memcmp(p, "v", 1) == 0)
相关文章:
- 在一次迭代中从 txt 文件中读取多行
- 从 C++ 中的标准输入迭代读取字节
- 如何从文件中读取 n 个字节并使用<uint8_t>迭代器将它们放入向量中?
- 无法使用迭代器在类对象的向量中读取向量
- 从相同纹理的撰写和读取OpenGL上的迭代de Solver
- 如何迭代在不知道其大小的情况下读取的字符串?
- 使用C++上的迭代读取和写入二进制文件
- 使用getline在文件中读取文件时,将值迭代值
- 输入迭代器可以重复读取,而输出迭代器只能写入一次
- 我可以读取向量<string>::迭代器的数字位置吗?
- 正确读取二进制文件并使用流迭代器
- 字符串迭代器与读取每一行不兼容
- 为什么用于解析我读取的行的内部循环每隔一次迭代就会通过外部读取循环跳过
- 使用迭代器数组读取稀疏信息
- 在内存中迭代以读取值
- Valgrind在简单迭代器类中检测到无效读取错误
- 读取大小为4的迭代器无效
- 我可以(a)不通过输入迭代器写入,或者(b)只通过一个迭代器读取一次吗?
- 迭代器应该读取整个数字,但它只读取第一位数字
- 使用迭代器读取格式化流是件好事吗