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;}