basic_istream::seekg() 似乎不起作用
basic_istream::seekg() seems to not be working
以下程序应输出如下内容:
Begin found
space found
End found
但事实并非如此。
#include <sstream>
#include <istream>
#include <string>
#include <cctype>
#include <iostream>
bool Match(std::istream& stream, const std::string& str)
{
std::istream::pos_type cursorPos = stream.tellg();
std::string readStr(str.size(),' ');
stream.read(&readStr[0],str.size());
stream.seekg(cursorPos);
if(std::size_t(stream.gcount()) < str.size() || readStr != str)
return false;
return true;
}
bool Take(std::istream& stream, const std::string& str)
{
if(!Match(stream,str))
return false;
for(std::string::size_type i = 0; i < str.size(); ++i)
stream.get();
return true;
}
int main()
{
std::string testFile = "BEGIN END";
std::stringstream ss(testFile);
auto c = ss.peek();
while(!ss.eof() && ss.tellg() != -1)
{
if(Take(ss,"BEGIN"))
std::cout << "Begin found" << std::endl;
else if(Take(ss,"END"))
std::cout << "End found" << std::endl;
else if(std::isspace(c))
{
ss.get();
std::cout << "space found" << std::endl;
}
else
std::cout << "Something else found" << std::endl;
}
return 0;
}
我注意到的是它输出
Begin found
Something else found
当我逐步使用调试器时,似乎当我达到空格字符时,它首先检查是否有要"BEGIN"
Match()
,它通过tellg()
检索光标位置,其值为5
。但是当它意外失败,然后用"END"
检查Match()
时,光标在-1
,即结束。
因此,seekg()
调用似乎不起作用,或者我没有正确使用它。
当你的程序进入主循环时,它首先以输入流和"BEGIN"作为参数执行 take。 匹配返回 true,get 被调用 5 次,因为这是 BEGIN 的长度。
然后它再次通过循环。 它再次调用匹配。 此时 pos 为 5,即 BEGIN 的长度。 它尝试读取 len(BEGIN( 字符,但字符串流没有那么多字符,因此它将循环保留在位置 -1,并设置错误标志。
由于流是错误状态,因此以下 seekg 调用不会产生预期的效果,从而解释程序的行为。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- basic_istream::seekg() 似乎不起作用
- Telling和seekg以及文件读取不起作用
- Seekg 在将 iStream 与自定义 Streambuf 一起使用时不起作用