c++ - 如何在 C++ 中将名称文件设置为变量?
问题描述
如何将“dt”变量设置为文件名?
time_t now = time(0);
string dt = ctime(&now);
ofstream file;
file.open( ??? );
我正在尝试将 dt 设置为名称
解决方案
从 C++11 开始,您可以简单地传递string
as 参数:file.open(dt);
.
在此之前,您必须传递一个const char*
as 参数:file.open(dt.c_str());
.
https://en.cppreference.com/w/cpp/io/basic_ofstream/open
您也可以直接使用构造函数:ofstream file(dt);
或ofstream file(dt.c_str());
在 C++11 之前。
https://en.cppreference.com/w/cpp/io/basic_ofstream/basic_ofstream
推荐阅读
- amazon-web-services - 就像 AWS Fargate 任务一样,在 Azure 上使用相同的 docker 映像运行多个容器实例
- java - h2 中的 onetoone 关系不会为一侧生成 id
- javascript - 我可以更改grapesjs中的工具栏语言吗?
- angular - Stencil 调用 Ionic Angular 子组件函数或生命周期钩子
- python - pyparsing:根据条件使用字符
- html - 如何更改各种 HTML 元素的强调色
- duplicates - 防止 Moodle 嵌入式答案中的重复(完形填空)
- java - Spring从GET url查询中设置@PostMapping字段?
- c - 我该如何解决这个错误?切换语句
- java - SpringBootTest 服务测试 - MockitoAnnotations Giving NoSuchMethodError: openMocks (java.lang.AutoCloseable)