自动创建文件名 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13108973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:58:18  来源:igfitidea点击:

Creating file names automatically C++

c++filenamesconst-char

提问by Tatiana

I'm trying to write a program in C++, which creates some files (.txt) and writes down the result in them. The problem is that an amount of these files is not fixed at the beginning and only appears near the end of the program. I would like to name these files as "file_1.txt", "file_2.txt", ..., "file_n.txt", where n is an integer.

我正在尝试用 C++ 编写一个程序,它创建一些文件 (.txt) 并在其中写下结果。问题是这些文件的数量在开始时不是固定的,只会在程序结束时出现。我想将这些文件命名为“file_1.txt”、“file_2.txt”、...、“file_n.txt”,其中 n 是一个整数。

I can't use concatenation because the file name requires type "const char*", and I didn't find any way to convert "string" to this type. I haven't found any answer through the Internet and shall be really happy if you help me.

我不能使用连接,因为文件名需要类型“const char*”,而且我没有找到任何方法将“string”转换为这种类型。我还没有通过互联网找到任何答案,如果你能帮助我,我会很高兴。

回答by Peter Alexander

You can get a const char*from an std::stringby using the c_strmember function.

您可以使用成员函数const char*从 an获取 a 。std::stringc_str

std::string s = ...;
const char* c = s.c_str();

If you don't want to use std::string(maybe you don't want to do memory allocations) then you can use snprintfto create a formatted string:

如果您不想使用std::string(也许您不想进行内存分配),那么您可以使用snprintf来创建一个格式化的字符串:

#include <cstdio>
...
char buffer[16]; // make sure it's big enough
snprintf(buffer, sizeof(buffer), "file_%d.txt", n);

nhere is the number in the filename.

n这是文件名中的数字。

回答by 111111

 for(int i=0; i!=n; ++i) {
     //create name
     std::string name="file_" + std::to_string(i) + ".txt"; // C++11 for std::to_string 
     //create file
     std::ofstream file(name);
     //if not C++11 then std::ostream file(name.c_str());
     //then do with file
 }

回答by Darren Smith

... another way to bulid the filename

...另一种建立文件名的方法

#include <sstream>

int n = 3;
std::ostringstream os;
os << "file_" << n << ".txt";

std::string s = os.str();

回答by Software_Designer

Sample code:

示例代码:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

string IntToStr(int n) 
{
    stringstream result;
    result << n;
    return result.str();
}

int main () 
{
    ofstream outFile;
    int Number_of_files=20;
    string filename;


  for (int i=0;i<Number_of_files;i++)
  {
        filename="file_" + IntToStr(i) +".txt";
        cout<< filename << "  \n";

        outFile.open(filename.c_str());
        outFile << filename<<" : Writing this to a file.\n";
        outFile.close();
  }


  return 0;
}

回答by user3728501

I use the following code for this, you may find this useful.

我为此使用以下代码,您可能会发现这很有用。

std::ofstream ofile;

for(unsigned int n = 0; ; ++ n)
{
    std::string fname = std::string("log") + std::tostring(n) << + std::string(".txt");

    std::ifstream ifile;
    ifile.open(fname.c_str());

    if(ifile.is_open())
    {
    }
    else
    {
        ofile.open(fname.c_str());
        break;
    }

    ifile.close();
}

if(!ofile.is_open())
{
    return -1;
}

ofile.close();