文件输入输出问题
lw1a2 2006-09-17 12:24:09 /*文件copy.out的内容:
abcd
efg
hi
j
计算每行的字节数,将结果加到最后一行,最后计算整个文件的字节数。
完成后的文件内容应该是:
abcd
efg
hi
j
5 9 12 14 24
*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream inOut("copy.out",ios_base::in|ios_base::app);
int cnt = 0;
char ch;
inOut.seekg(0);
while ( inOut.get( ch ))
{
cout.put( ch );
cnt++;
if ( ch == '\n' )
{
ios::pos_type mark = inOut.tellg();//保存文件当前位置
inOut.seekg(0,ios_base::end);//写在文件尾
inOut << cnt << ' ';
inOut.seekg( mark );//恢复位置
}
}
inOut.clear();
inOut << cnt << endl;
cout << "[ " << cnt << " ]"<<endl;
system("pause");
return 0;
}
--------------------------------------------------------
以上这段代码,在VC2005里正确执行,在DevCpp4.9.9.2里,文件没有改变。
大家看看有什么问题