解析流
我正在解析一个包含字符串和数值的文件。我想逐个字段处理文件,每个字段都由空格或行尾字符分隔。 ifstream::getline() 操作仅允许单个分隔字符。因此,我当前所做的是以字符 ' ' 作为分隔符的 getline,然后如果遇到 '\n' 则手动返回到流中的上一个位置:
ifstream ifs ( filename , ifstream::in );
streampos pos;
while (ifs.good())
{
char curField[255];
pos = ifs.tellg();
ifs.getline(curField, 255, ' ');
string s(curField);
if (s.find("\n")!=string::npos)
{
ifs.seekg(pos);
ifs.getline(curField, 255, '\n');
s = string(curField);
}
// process the field contained in the string s...
}
但是,“seekg”似乎定位了流一个字符太晚了(因此我错过了每个换行符之前每个字段的第一个字符)。 我知道还有其他方法可以通过逐行扫描等方式来编码这样的解析器,但我真的很想了解为什么这段特定的代码失败...
非常感谢!
I am parsing a file which contains both strings and numerical values. I'd like to process the file field by field, each delimited by a space or an end-of-line character.
The ifstream::getline() operation only allows a single delimiting character. What I currently do is thus a getline with the character ' ' as a delimiter, and then manually go back to the previous position in the stream if a '\n' has been encountered :
ifstream ifs ( filename , ifstream::in );
streampos pos;
while (ifs.good())
{
char curField[255];
pos = ifs.tellg();
ifs.getline(curField, 255, ' ');
string s(curField);
if (s.find("\n")!=string::npos)
{
ifs.seekg(pos);
ifs.getline(curField, 255, '\n');
s = string(curField);
}
// process the field contained in the string s...
}
However, the "seekg" seems to position the stream one character too late (I thus miss the first character of each field before each line break).
I know there are other ways to code such a parser, by scanning line by line etc.., but I'd really like to understand why this particular piece of code fails...
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Loadmaster 所说,可能存在下落不明的字符,或者这可能只是一个相差一的错误。
但这只是必须说的......你可以
用这个替换这个:
得到你想要的行为。
As Loadmaster said, there may be unaccounted for characters, or this could just be an off-by-one error.
But this just has to be said... you can replace this:
With this:
To get the behavior you want.
输入流中可能存在前瞻/后推字符。 IIRC,查找/告知功能不知道这一点。
There may be a look-ahead/push-back character in the input stream. IIRC, the seek/tell functions are not aware of this.