C++ 如何保存文本文件而不键入“quot;”;。txt";最后?

C++ 如何保存文本文件而不键入“quot;”;。txt";最后?,c++,C++,我的程序(c++): #包括 #包括 #包括 使用名称空间std; 浮动x,y,z; chard[20]; int main() { cin.getline>>d; x=111; y=222; z=333; 梅亚尔基沃流; meuarquivo.open(d“.txt”); meuarquivo使用std::string,它为串联定义了运算符+: #include <iostream> #include <fstream> #include <cstdlib&

我的程序(c++):

#包括
#包括
#包括
使用名称空间std;
浮动x,y,z;
chard[20];
int main()
{   
cin.getline>>d;
x=111;
y=222;
z=333;
梅亚尔基沃流;
meuarquivo.open(d“.txt”);

meuarquivo使用
std::string
,它为串联定义了
运算符+

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

int main()
{   
  std::string filename;
  std::getline(std::cin, filename);

  std::ofstream file((filename + ".txt").c_str());

  // use stream here.
}
#包括
#包括
#包括
#包括
int main()
{   
std::字符串文件名;
std::getline(std::cin,文件名);
std::of流文件((filename+“.txt”).c_str());
//在这里使用流。
}

您使用char[]保存输入有什么特殊原因吗?您不能使用标准库中的字符串吗?不幸的是,您需要
std::ofstream文件((filename+“.txt”).c_str())
。这将很快用c++0x解决。一些编译器已经有了它。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

int main()
{   
  std::string filename;
  std::getline(std::cin, filename);

  std::ofstream file((filename + ".txt").c_str());

  // use stream here.
}