
C++ Exercises(九)
<<C++ Primer>>第三版P905页的这个程序有很多问题想不明白: #include <iostream> #include <fstream> using namespace std;
int main() { fstream inOut( "D:\copy.out", ios_base::in|ios_base::app); int cnt=0; char ch; inOut.seekg(0); while ( ( ch ) ) { cout.put( ch ); cnt++; if ( ch == 'n' ) { streamoff mark = llg();// 标记当前位置 inOut << cnt << ' '; inOut.seekg( mark ); // 恢复位置 } } inOut.clear(); inOut << cnt << endl; cout << "[ " << cnt << " ]n"; return 0; }
用下面的数据进行测试: abcd efg hi j
分别保存为和copy.out,运行结果: abcd efg hi [ 12 ]
后来我想可能是因为不是读的二进制的缘故,所以我改为: fstream inOut( "D:\", ios_base::in|ios_base::app|ios_base::binary);
分别进行测试后,结果就更奇怪了。 这是的: abcd efg hi [ 15 ]
这是copy.out的: abcd efg j [ 18 ] 这到底是为什么呢?
本文转自Phinecos(洞庭散人)博客园博客,原文链接:.html,如需转载请自行联系原作者