#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string name;
cout<<"What would you like new html file to be named?"<<endl;
getline(cin,name);
cout<<"Creating New Html File...Moment."<<endl;
ofstream myfile (name);
if(myfile.is_open())
{
}
}
我需要使用.html扩展名创建myfile,有人可以告诉我如何编写代码吗?
答案 0 :(得分:8)
string name;
cout<<"What would you like new html file to be named?"<<endl;
getline(cin,name);
cout<<"Creating New Html File...Moment."<<endl;
name+=".html"; // the crucial ommision?
ofstream myfile (name);
答案 1 :(得分:1)
您只需将.html
添加到文件名的末尾:
name.append(".html");
答案 2 :(得分:0)
您是否想要考虑用户输入的文件名已经以“.html”结尾的可能性?在这种情况下,您可以在检查到名称中至少有6个字符后,使用name.substr(name.size()-5)
获得后缀。