在内存中迭代以读取值

iterating through memory to reading values

本文关键字:读取 迭代 内存      更新时间:2023-10-16

我有一个将文本文件读入内存的当前代码:

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)