我正在尝试使用 C++ 为命令提示符制作一种记事本。我希望有人能够输入他们想要的文件名称以及文件中的内容。我该怎么做?这是我的:
printf("What do you want to call it?\n");
scanf_s("%s", &filename);
ofstream myfile;
myfile.open("%s", filename);
myfile.close();
return 0;
如果需要,您可以对其进行编辑或从头开始制作。 另外,请帮我让他们在里面输入他们想要的内容。
最佳答案
这是我认为您正在寻找的代码:
#include <string>
#include <iostream>
#include <fstream>
int main(void)
{
std::string filename;
std::cout << "Enter filename to create: ";
std::cin >> filename;
std::ofstream my_file(filename.c_str());
if (my_file)
{
my_file << "Sample text\n";
my_file.close();
}
return 0;
}
关于c++ - 如何使用 scanf 命名文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374829/