解析流

发布于 2024-09-26 10:04:04 字数 687 浏览 7 评论 0原文

我正在解析一个包含字符串和数值的文件。我想逐个字段处理文件,每个字段都由空格或行尾字符分隔。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风流物 2024-10-03 10:04:04

正如 Loadmaster 所说,可能存在下落不明的字符,或者这可能只是一个相差一的错误。

但这只是必须说的......你可以

 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...
 }

用这个替换这个:

 ifstream ifs ( filename , ifstream::in );
 streampos pos;
 string s;
 while (ifs.good())
 {  
   ifs >> s;
   // process the field contained in the string s...
 }

得到你想要的行为。

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:

 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...
 }

With this:

 ifstream ifs ( filename , ifstream::in );
 streampos pos;
 string s;
 while (ifs.good())
 {  
   ifs >> s;
   // process the field contained in the string s...
 }

To get the behavior you want.

小伙你站住 2024-10-03 10:04:04

输入流中可能存在前瞻/后推字符。 IIRC,查找/告知功能不知道这一点。

There may be a look-ahead/push-back character in the input stream. IIRC, the seek/tell functions are not aware of this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文