如何制作文件名和设置扩展名

0 c++

#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())
  {                
  }
}
Run Code Online (Sandbox Code Playgroud)

我需要使用.html扩展名制作myfile,有人可以告诉我如何编写代码吗?

180*_*ION 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);
Run Code Online (Sandbox Code Playgroud)

  • 检查给定名称本身不以".html"结尾可能是明智的,因此您不会创建"file.html.html". (2认同)