首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++文件小疑点

2012-04-24 
C++文件小问题#includefstream#includeiostream#includestringusing namespace stdvoid main(){fst

C++文件小问题
#include"fstream"
#include"iostream"
#include<string>
using namespace std;
void main()
{
fstream file;
char *name;
cin>>name;
file.open(name,ios::out);
if(file.fail())cout<<"s ";
}

编译通过运行出错。哪里不对头?

[解决办法]
char *name;野指针
改为
char name[64];


[解决办法]

C/C++ code
#include <iostream>#include <fstream>#include <string>using namespace std;int main(){    string name;    //你都定义了string文件头干么不用string?    cin >> name;    ofstream file(name.c_str()); //你这要定义ofstream不是fstream,默认就是ios::out;    if(file.fail())    {        cout << "s";    }    return 0;} 

热点排行