streamseekg()的实现在VS2012到2010之间有所不同
stream seekg() implementation varies from VS2012 to 2010
我有一段旧代码,它在gcc VS2010上运行良好。我试图在VS2012中编译相同的代码。我知道我可以在任何地方设置为true,但这不是实际的代码。我缩短了代码以重现问题。
std::ifstream file_stream;
file_stream.open("C:\experiment\file.txt", std::ios_base::in);
std::istream& stream = file_stream;
bool done = false;
while(stream.good() || !done){
int stream_position = stream.tellg();
bool stream_failure = (stream_position == -1);
bool stream_eof = stream.eof();
std::string line;
std::getline(stream, line);
std::cout << stream_failure << stream_eof << std::endl;
std::streampos pos = stream.tellg();
if(pos == std::streampos(-1)){
std::streampos copy = pos;
stream.seekg(0, std::ios_base::end);
pos = stream.tellg();
stream.seekg(copy);
}
}
std::getline(std::cin, std::string());
file_stream.close();
如果我将平台工具集更改为VS2010,它会工作并打印1
而不是stream_eof
如果我将平台工具集更改为VS2012,则不会,并打印0
而不是stream_eof
在到达EOF 之后
如果我在cout后面放一个if(stream_eof)return 0;
,它会在VS2010上返回,但不会在VS2012 中返回
这是C++03和C++11之间的区别之一。
在C++03中,当您在设置了任何错误位的流上调用seekg()
时,它将失败,并且不执行任何其他操作。
在C++11中,seekg()
首先无条件地清除eofbit
,然后尝试按指示执行。在这种情况下,由于failbit
也被设置,因此它失败,但eofbit
被清除。
(顺便说一句,为什么你的循环首先被设置为读取文件末尾?使用通常的while(getline(stream, line))
)
相关文章:
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- int(c) 和 c-'0' 之间的区别。C++
- 在cuda线程之间共享大量常量数据
- 在c代码之间共享数据的最佳方式
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- C++ 使用 assign 函数的字符串与直接使用 '=' 更改值的字符串之间的区别
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- std::atomic和std::condition_variable wait,notify_*方法之间的区别
- 大小相等但成员数量不同的结构之间的性能差异
- 类与私有变量的其他类之间的线程安全性
- 如何在cpp文件之间切换窗口?在Qt中
- 线程之间的布尔停止信号
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- MSVC 2010 编译应用程序和 MSVC 2019 编译应用程序之间的行为差异
- 转换 VS 2010 和 2015 之间的差异
- 使用Visual Studio 2010的win32和x64之间的生成时间差异很大
- 编译器(Visual Studio 2010和GCC)之间的浮点不匹配
- Visual studio 2010-在不同的生成配置之间保留项目属性
- streamseekg()的实现在VS2012到2010之间有所不同
- VS 2010 和 VS 2010 SP1 C++二进制文件之间的二进制兼容性