大众信息网

VS2010上C++文件的输入输出问题。

关注:79  答案:2  手机版
解决时间 2021-04-04 10:01
我的源程序是这样的:
#include
#include
#include
using namespace std;
int main()
{
ostream outfile("1.dat",ios::out|ios::in);
if(!outfile)
cout<<"open error!"<else
cout<<"open succeed!"<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
#include
#include
#include
using namespace std;
int main()
{
ostream outfile(_T("1.dat"),ios::out|ios::in);
if(!outfile)
cout<<"open error!"<else
cout<<"open succeed!"<system("pause");
return 0;
}
可是还是出现相同的问题,我崩溃了,求大神解决!
最佳答案
ostream outfile("1.dat",ios::out|ios::in);//改成下面

ostream outfile("1.dat",ios::out);
全部回答
我也用vc2010,但不会出这个错误 所给代码,除了最后“retur你0;”原为return 0;之外,没有错误,可以通过编译和运行。 错误原因很可能是在项目设置上的: 建议重新建一个vc->win console的项目,应该能消除这个问题。
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!