VS2010上C++文件的输入输出问题。
我的源程序是这样的:
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
ostream outfile("1.dat",ios::out|ios::in);
if(!outfile)
cout<<"open error!"<<endl;
else
cout<<"open succeed!"<<endl;
system("pause");
return 0;
}
编译后的错误是这样的:
错误1error C2664: “std::basic_ostream<_Elem,_Traits>::basic_ostream(std::basic_streambuf<_Elem,_Traits> *,bool)”: 不能将参数 1 从“const char [6]”转换为“std::basic_streambuf<_Elem,_Traits> *”h:\c++\test-13\test-13\test-13-2.cpp71test-13
然后我试着把项目的字符集从unicode改为多字符集,还是会出现相同的错误,接着又改了一下源程序,添加了tchar.h头文件:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <tchar.h>
using namespace std;
int main()
{
ostream outfile(_T("1.dat"),ios::out|ios::in);
if(!outfile)
cout<<"open error!"<<endl;
else
cout<<"open succeed!"<<endl;
system("pause");
return 0;
}
可是还是出现相同的问题,我崩溃了,求大神解决!
[解决办法]
- C/C++ code
ostream outfile("1.dat",ios::out|ios::in);
[解决办法]
是ofstream而不是ostream,参考以下用法:
- C/C++ code
#include <fstream>#include <iostream>using namespace std;int main(int argc, char* argv[]){ ofstream outfile; outfile.open("1.dat"); if(!outfile) { cout << "open error!" << endl; } else { cout << "open succeed!" << endl; } system("pause"); return 0;}
[解决办法]
ostream 换成 fstream
[解决办法]