要求用户在C++中创建文本文件时输入文本文件(ASCII)名称



我想让用户在C++控制台应用程序(WINDOWS 7)中输入文件名(不是二进制文件,而是文本文件),在用户在char name[10]变量中输入名称后,我想将此名称分配给文本文件

fout.open("<user_name_here>.txt",ios::out); 
> where <user_name_here> is a name entered by user

考虑到我是初学者,请回答这个问题

Cin可用于从标准输入(键盘)读取。

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main () {
   string name;
   cin >> name; //name should not contain whitespaces/tabs etc
   ofstream out_file;
   out_file.open (name.c_str());
   out_file << "I am writing something to the file";
   out_file.close();
}

最新更新