代码之家  ›  专栏  ›  技术社区  ›  jo

如何生成文件名和集扩展名

c++
  •  0
  • jo  · 技术社区  · 15 年前
    #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的文件,有人能告诉我怎么做或者给我写一个代码吗?

    3 回复  |  直到 15 年前
        1
  •  8
  •   1800 INFORMATION    15 年前
    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);
    
        2
  •  1
  •   Zifre    15 年前

    你只需要添加 .html 文件名末尾:

    name.append(".html");
    
        3
  •  0
  •   MSalters    15 年前

    name.substr(name.size()-5) -当然,在您检查了名称中至少有6个字符之后。