For example if I have this int type variable X that increment from 1 at the beginning to 10 when the program ends, how to open and write to 10 different files (data1.txt,data2.txt,data3.txt....) everytime X increments?

I've tried with the following but it didn't work:

string filename ("data");
int X;
ofstream out;
char number;
for(X=1;X<11;X++)
{
number=X;
filename+=number;
out.open(filename.c_str());
....
...writing....

out.close();
}

Recommended Answers

All 4 Replies

you are very close to getting ti working the problem is how you are converting an int to char assuming that you are only going from 0 to 9 you can have

//this is either the relative or absolute file path
std::string base_file = "data";
std::string after_number = ".txt"
for(int fid(0); fid < 10; ++fid)
{
 char digit = '0'  + fid;
 std::string cur_filename = base_file + digit + after_number;
 std::ifstream fin(cur_filename.c_str());
 if(fin.is_open() == true)
 {
    //etc..
    fin.close();
 }
 else
 {
     std::cout << "could not open file:" << cur_filename << std::endl;
  }
 
}

the digit code will need expanding for larger number of files but it is just a file manipulation check what the string looks like before opening.

For example if I have this int type variable X that increment from 1 at the beginning to 10 when the program ends, how to open and write to 10 different files (data1.txt,data2.txt,data3.txt....) everytime X increments?

I've tried with the following but it didn't work:

string filename ("data");
int X;
ofstream out;
char number;
for(X=1;X<11;X++)
{
number=X;
filename+=number;
out.open(filename.c_str());
....
...writing....

out.close();
}

Do note that your filename variable is in the loop; which will make the name keep on expanding. E.g. data1, data12, data123, ...
You have to reset it to "data" after the output stream is closed. Also, you need to append the file extension name (in your case, the .txt)

You might use a stringstream for that purpose. This has been answered probably a gazillion times, here we go again ..

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::string filename ("data");
  std::string extension (".txt");
  std::stringstream sstrm;

  for(int X=1;X<11;X++)
  {
    sstrm << filename << X << extension;
    std::cout << sstrm.str().c_str() << std::endl;
    // Get rid of the current content ..
    sstrm.str("");    
  }

  return 0;
}

You might use a stringstream for that purpose. This has been answered probably a gazillion times, here we go again ..

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::string filename ("data");
  std::string extension (".txt");
  std::stringstream sstrm;

  for(int X=1;X<11;X++)
  {
    sstrm << filename << X << extension;
    std::cout << sstrm.str().c_str() << std::endl;
    // Get rid of the current content ..
    sstrm.str("");    
  }

  return 0;
}

I expected this to be easy for a lot of people but strangely didn't find any specific solution after searching the Internet for a while. Thanks for the patience.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.