读书人

C++程序有关问题下面的程序是输出wr

发布时间: 2012-03-27 13:44:24 作者: rapoo

C++程序问题,下面的程序是输出wr1.txt文件中的数据,但编译时发现if(f1>>i)这句有问题,请问要怎样改???
错误提示:error C2676: binary '>>' : 'class ofstream' does not define this operator or a conversion to a type acceptable to the predefined operator



#include <iostream.h>

#include <stdlib.h>
#include <fstream.h>

void main()
{
ofstream f1("wr1.txt", ios::in|ios::nocreate);
if(!f1)
{
cerr<<"File 'wr1.txt' not found";
exit(1);
}

int i;
while(!f1.eof())
{
if(f1>>i)
cout<<i<<" ";
}
cout<<endl;
f1.close();
}

[解决办法]
ofstream 改成ifstream
[解决办法]
f1<<i
[解决办法]

探讨

ofstream 改成ifstream

读书人网 >C++

热点推荐