【发布时间】:2020-07-06 18:18:52
【问题描述】:
我一直在尝试如何让用户输入来命名将要创建的文件,我一直在尝试制作一个可以做笔记并将它们存储在文件中的日记应用程序。反正这里是代码
#include "main.h"
#include <string>
char Start;
int main()
{
int input;
std::fstream fFile;
std::cout << "*******" << std::endl;
std::cout << "Journal" << std::endl;
std::cout << "*******" << std::endl;
std::cout << "Enter any value to continue..." << std::endl;
std::cin >> Start;
system("CLS");
std::cout << "Hello" << std::endl;
std::cout << "....." << std::endl;
std::cout << "" << std::endl;
std::cout << "[1] Create New Note" << std::endl;
std::cout << "[2] Browse Note Libary" << std::endl;
std::cin >> input;
if (input == 1)
{
std::string NoteName;
std::cout << "Enter New Notes Name" << std::endl;
std::cin >> NoteName;
fFile.open("NoteName.txt"); // Here is what I can't figure out
fFile << "test" << std::endl;
fFile.close;
}
if (input == 2)
{
}
}
Main.h 只包含 include 并生成它,以便我可以调用 main 函数 (:
【问题讨论】:
-
只需使用变量
NoteName而不是文本字符串"NoteName.txt",它应该可以工作。只是不要忘记检查该文件是否成功打开,因为它可能不是出于其他原因,包括无效名称。
标签: c++ fstream iostream journal