Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动创建文件名C++; 我试图用C++编写一个程序,它创建一些文件(.txt)并将结果写入其中。问题在于,这些文件的数量在程序开始时并不是固定的,只是在接近程序结束时出现。我想把这些文件命名为“file_1.txt”、“file_2.txt”、“file_n.txt”,其中n是一个整数_C++_File_Names_Const Char - Fatal编程技术网

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

自动创建文件名C++; 我试图用C++编写一个程序,它创建一些文件(.txt)并将结果写入其中。问题在于,这些文件的数量在程序开始时并不是固定的,只是在接近程序结束时出现。我想把这些文件命名为“file_1.txt”、“file_2.txt”、“file_n.txt”,其中n是一个整数,c++,file,names,const-char,C++,File,Names,Const Char,我不能使用连接,因为文件名需要类型“const char*”,并且我没有找到任何方法将“string”转换为这种类型。我在网上没有找到任何答案,如果你能帮助我,我将非常高兴。你可以通过使用成员函数从std::string获得const char* std::string s = ...; const char* c = s.c_str(); 如果不想使用std::string(可能不想进行内存分配),则可以使用创建格式化字符串: #include <cstdio> ... char

我不能使用连接,因为文件名需要类型“const char*”,并且我没有找到任何方法将“string”转换为这种类型。我在网上没有找到任何答案,如果你能帮助我,我将非常高兴。

你可以通过使用成员函数从
std::string
获得
const char*

std::string s = ...;
const char* c = s.c_str();
如果不想使用
std::string
(可能不想进行内存分配),则可以使用创建格式化字符串:

#include <cstdio>
...
char buffer[16]; // make sure it's big enough
snprintf(buffer, sizeof(buffer), "file_%d.txt", n);
#包括
...
字符缓冲区[16];//确保它足够大
snprintf(buffer,sizeof(buffer),“file_%d.txt”,n);

n
这是文件名中的数字。

。。。创建文件名的另一种方法

 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
 }
#include <sstream>

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

std::string s = os.str();
#包括
int n=3;
std::ostringstream os;
os示例代码:

#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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
字符串IntToStr(int n)
{
流结果;

结果我使用下面的代码,您可能会发现这很有用

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();

你看过
c_str
函数了吗?仅仅因为文件名最终需要是
const char*
并不意味着你在整个程序中都要使用它。你应该尽可能多地使用
std::string
,然后只有在真正使用它的时候才从它那里获得
const char*
。关于内存分配的注意事项
std::string
:许多实现都会对小字符串进行优化,因为小字符串不会分配内存,但这并不能保证。@PeterAlexander:好吧,MSVC的dirkumware版本有SSO,而gcc的libstdc++没有。我认为clang的libc++也有SSO,但是因为它几乎没有移植到OS X之外,所以它没有什么意义大多数人都感兴趣。