Mr. Confused
Mr. Confused

Reputation: 255

File naming in C++

I have written a program in c++ that runs a loop. Whenever the loop ends the value of a matrix, say A[i][j], changes using a numerical method.

What I typically do is output this latest value of A at particular loop counter using if-else or switch statement which outputs the value in a particular file mentioned in the if-else statement. However, if I want to get the matrix value at each loop counter how do I that?

I can't write if conditions for all. Please help, or better, if possible provide a pseudo code so that for a loop counter value (lets say i) of 1 filename is 1.txt (1.dat) and for i=2 file name is 2.dat and so on.

I have added a sample code as to what i want.

int main()
{
    string str="";
    int a[10];
    for(int i=0;i<10;i++)
    {
        a[i]=i;
    }

    for(int t=1;t<16;t++)
    {
          str=t;
          ofstream data("str.dat");
          if(data.is_open())
          {
              cout << "File Opened successfully!!!. Writing data from array to file" << endl;
              for(int i=0;i<10;i++)
              {
                  data<<a[i]<<endl;
              }
          }
          else //file didn't open
          {
              cout << "File could not be opened." << endl;
          }

          for(int m=0;m<10;m++)
          {
              a[m]=2*a[m];
          }

    }

    return 0;
}

In this code all i get is a file named str.dat. I want it to be 1.dat, 2.dat, 3.dat and so by passing values of t to string str and then have files the number of times t is run.

I hope this will help in understanding the problem.

Upvotes: 0

Views: 581

Answers (3)

Mr. Confused
Mr. Confused

Reputation: 255

Thanks for all the help.....finally got it(with some help).....am pasting it here in case someone needs it in future.......

int main()
{
    int a[10];
    for(int i=0;i<10;i++)
    {
        a[i]=i;
    }

    for(int t=1;t<15;t++)
    {
          string name="";
          name+=to_string(t);
          ofstream data(name.c_str());
          if(data.is_open())
          {
              cout << "File Opened successfully!!!. Writing data from array to file" << endl;
              for(int i=0;i<10;i++)
              {
                  data<<a[i]<<endl;
              }
          }
          else //file didn't open
          {
              cout << "File could not be opened." << endl;
          }

          for(int m=0;m<10;m++)
          {
              a[m]=2*a[m];
          }

    }

    return 0;
}

Upvotes: 0

Slava
Slava

Reputation: 44258

As simple as this:

for( int i = 0; i < someValue; ++i ) {
    std::string filename = std::to_string( i ) + ".txt";
    ...
}

Upvotes: 1

I believe you're looking for this:

#include <sstream>

// ...

std::ostringstream filename;
filename << "File-" << i << ".dat";
std::ofstream file(filename.str().c_str());

This uses a string stream (output to a string) to construct the filename from fixed text and the value of i. Then, the constructed string is extracted from the string stream using its member function .str(), and passed as a parameter to the constructor of a file output stream. This means it will open the appropriate file.

Upvotes: 0

Related Questions