Creating file names automatically C++
Asked Answered
C

5

5

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.

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.

Carltoncarly answered 28/10, 2012 at 12:58 Comment(2)
Have you looked at the c_str function? en.cppreference.com/w/cpp/string/basic_string/c_strSubjugate
Just because the filename needs to be a const char* in the end doesn't mean you're stuck with it for your whole program. You should use std::string as much as you can, then obtain a const char* from it only when you really it.Scheller
S
7

You can get a const char* from an std::string by using the c_str member function.

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 snprintf to create a formatted string:

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

n here is the number in the filename.

Sylph answered 28/10, 2012 at 12:59 Comment(2)
A note on the memory allocations of std::string: many implementations do the small string optimisation where small strings won't allocate memory, however this isn't guaranteed.Sylph
@PeterAlexander: Well, MSVC's dirkumware version has SSO while gcc's libstdc++ has not. I think that clang's libc++ has SSO too, however since it's barely ported outside of OS X it's of little interest to most.Mayorga
W
7
 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
 }
Woodbridge answered 28/10, 2012 at 13:4 Comment(0)
P
3

... another way to bulid the filename

#include <sstream>

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

std::string s = os.str();
Pro answered 28/10, 2012 at 13:6 Comment(0)
S
1

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;
}
Subminiaturize answered 28/10, 2012 at 19:12 Comment(0)
J
1

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();
Jacket answered 16/8, 2015 at 14:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.