如何连接路径和不同的文件名 - C++
How to concatenate path and varying file names - C++
这是我的代码:
double loglikelihood = 0;
double loglikelihood1 = 0;
double THRESHOLD = 5;
double c = THRESHOLD + 1;
std::ofstream llh_file;
std::ofstream myfile;
const char *path= "path_of_file_to_be_saved";
myfile = ( string(path) + flags.inference_result_file_.c_str() );
for (int iter = 0; c > THRESHOLD; ++iter) {
std::cout << "Iteration " << iter << " ...n";
loglikelihood = 0;
llh_file.open(myfile.c_str() );
loglikelihood += sampler.LogLikelihood(&document);
llh_file << "THE LOGLIKELIHOOD FOR ITER " << iter << " " << "IS: " << loglikelihood << "n";
llh_file.close();
我是C++的新手。我有一个包含不同文件名的文件夹。我想在 for 循环中执行一些过程,并将结果保存在文件夹中,并将确切的文件名作为输入文件。我该怎么做?请帮忙!
连接字符串。使用 std::string 而不是 char*。喜欢:
#include <string>
#include <iostream>
#include <fstream>
int main() {
std::string path = "path";
std::string file = "file.txt";
std::string myfile = path + "/" + "file.txt";
std::string fname = "test.txt";
std::ofstream f(fname);
f << myfile;
}
这会在名为 test.txt 的文件中写入"path/file.txt"。
相关文章:
- 从命令行c++发送文本文件名
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么文件名被设置为一个点,而不是在读取矢量中的文件名时
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 如何在CPP的给定目录中列出UTF编码的文件名?
- 在Visual Studio 2019中保存时ReadDirectoryChangesW文件名的问题
- C++:在Windows中浏览名称中带有点的文件名
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 不带预处理器的调用方法/文件的文件名/行号
- 在 C++ 中为文件名添加时间戳
- 为什么当我在 std::string 中打开文件名的 fstream 时出现"no matching function"错误?
- 如何将文件名作为参数传入并正确打开?
- 如何在目录及其子文件夹中构建文件名字符串向量?
- 要求用户输入C++输出文件名
- 从Arduino SD卡模块到Array的文件名
- 如何使用打印到 PDF 打印机在 MFC 和 CView 中以编程方式打印而不提示输入文件名?
- C++ 从变量导入文件名
- 从 Makefile 中的 $@ 获取文件名
- VSCode C++调试文件名中包含中文字符的文件时插件损坏
- _CrtSetAllocHook - 空文件名和行号参数