使用 ifstream 解析文本的惯用方法是什么?
我正在尝试解析文本文件以查找模式,然后获取子字符串。该代码片段工作正常,但是我可以改进吗?我可以尽量减少这里的复制吗?即我得到一行并将其存储在buf中然后构造一个字符串,可以消除这种复制吗?
简而言之,实现这一目标的惯用方法是什么?
std::ifstream f("/file/on/disk");
while (!f.eof()) {
char buf[256];
f.getline(buf, sizeof(buf));
std::string str(buf);
if (str.find(pattern) != std::string::npos)
{
// further processing, then break out of the while loop and return.
}
}
I'm trying to parse a text file to find a pattern then grab a substring. This code fragment works fine, however can I improve this? Can I minimize copying here? I.e. I get a line and store it in the buf then construct a string, can this copying be eliminated?
In short what's the idiomatic way of achieving this?
std::ifstream f("/file/on/disk");
while (!f.eof()) {
char buf[256];
f.getline(buf, sizeof(buf));
std::string str(buf);
if (str.find(pattern) != std::string::npos)
{
// further processing, then break out of the while loop and return.
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一种可能的重写:
主要更改被标记为内联,但总结一下:
std::string
;只需使用strstr
进行扫描即可。进一步说明,您可能不想在此处使用 C 样式字符串,除非您确定这就是您想要的。 C++
string
可能更好:Here's one possible rewrite:
The main changes are marked inline, but to summarize:
std::string
; just usestrstr
to do the scan.As a further note, you probably don't want to use a C-style string here unless you're sure that's what you want. A C++
string
is probably better:在代码中,首先将文件中的字符复制到
char
数组中。这应该是所有必要的复制。如果您需要阅读每个字符一次,那么甚至不需要该副本。接下来,从您填充的数组构造一个
std::string
。再说一遍,没有必要。如果您想要一个字符串,请直接从流中复制到字符串中。In your code, you first copy characters from the file into a
char
array. That should be all the copying necessary. If you'd need to read each character once then even that copy wouldn't be necessary.Next, you construct a
std::string
from the array you filled. Again, unnecessary. If you want a string then copy from the stream directly into a string.您根本不需要那个
char[]
。You don't need that
char[]
at all.