<<C++ Primer>>第三版P905页的这个程序有很多问题想不明白:
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
#include
<
iostream
>
#include < fstream >
using namespace std;
int main()
{
fstreaminOut("D://copy.out",ios_base::in|ios_base::app);
intcnt=0;
charch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
cnt++;
if(ch=='/n')
{
streamoffmark=inOut.tellg();//标记当前位置
inOut<<cnt<<'';
inOut.seekg(mark);//恢复位置
}
}
inOut.clear();
inOut<<cnt<<endl;
cout<<"["<<cnt<<"]/n";
return0;
}
#include < fstream >
using namespace std;
int main()
{
fstreaminOut("D://copy.out",ios_base::in|ios_base::app);
intcnt=0;
charch;
inOut.seekg(0);
while(inOut.get(ch))
{
cout.put(ch);
cnt++;
if(ch=='/n')
{
streamoffmark=inOut.tellg();//标记当前位置
inOut<<cnt<<'';
inOut.seekg(mark);//恢复位置
}
}
inOut.clear();
inOut<<cnt<<endl;
cout<<"["<<cnt<<"]/n";
return0;
}
用下面的数据进行测试:
abcd
efg
hi
j
efg
hi
j
分别保存为
abcd
efg
hi
[12]
efg
hi
[12]
后来我想可能是因为不是读的二进制的缘故,所以我改为:
fstreaminOut(
"
D://data.txt
"
,ios_base::
in
|
ios_base::app
|
ios_base::binary);
分别进行测试后,结果就更奇怪了。
这是data.txt的:
abcd
efg
hi
[15]
efg
hi
[15]
这是copy.out的:
abcd
efg
j
[18]
efg
j
[18]
这到底是为什么呢?