遍历内存读取值
我有将文本文件读入内存的当前代码:
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);
}
我知道必须有更好/更安全的方法来做到这一点。
I have the current code that readings a text file into memory:
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;
Now i have code that iterates over it. If the line starts with 'v' it reads the 3 preceding float values, and same with if it starts with 'n', but into different arrays.
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);
}
I know there must be a better/safer way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设你那里有一个文本文件。这可以做得更简单。首先,不要以二进制模式打开文件,而只是按行读取。以下是一个可能的实现:
此代码假定以
'v'
开头的行格式良好。你可以这样使用它:I assume you have a text-file there. This can be done a lot simpler. First, don't open the file in binary-mode, and simply read linewise. The following is a possible implementation:
This code assumes that lines starting with
'v'
are well-formed. You can use it like this:如果您的系统支持(即任何类似 *nix 的系统),请使用 mmap。这将(很快)为您提供文件内容的
char*
。如果您的文件很大,那么这非常有用,因为它使用虚拟内存系统为您缓存所有内容 - 即您不必等待所有数据都被复制。 mmap 函数立即返回,并且虚拟内存的相关部分已经映射到您的文件。最后,
memcmp
的长度为 1 似乎有点毫无意义。为什么不直接使用if(*p=='v')
而不是if(memcmp( p, "v", 1) == 0)
Use mmap if your system supports it (i.e. any *nix-like system). This will (very quickly) give you a
char*
to the file contents. If your file is big, this works great as it uses the virtual memory system to cache everything for you - i.e you don't have to wait while all the data is copied around. The mmap function returns instantly and the relevant section of virtual memory is already mapped to the your file.Finally,
memcmp
seems a bit pointless with a length of 1. Why not just toif(*p=='v')
instead ofif(memcmp(p, "v", 1) == 0)