-2

I'm trying to increment the name of the file based on the number associated with the filename. If I make the variable postFix an int type, it gives an error (understandably) since I can't concatenate an int to a string type.

In that case how do I increment string variable?

for (int i = 0; i <= 10; i++) 
{
  std::string postFix = "1"; // int postFix = 1 gives an error
  std::cout << (std::string("Image" + postFix + std::string(".png")));
}
3
  • 1
    Please clarify "incrementing" a string variable. What is the value of a string after it is incremented? If I have the string "hello", what happens after it is incremented? Commented Jun 12, 2015 at 20:14
  • Another suggestion, you can make the filenames unique by adding a timestamp to the filename. Commented Jun 12, 2015 at 20:16
  • @ThomasMatthews I want to increment the postFix value from 1 to 2 to 3 to.. 20 as for loop ends. Commented Jun 12, 2015 at 20:17

7 Answers 7

3

You can use std::to_string to create a std::string from your int value.

for (int i = 0; i <= 10; i++) 
{
    std::cout << "Image" + std::to_string(i+1) + ".png";
}

Although in this case you might as well just use the stream operator<<

for (int i = 0; i <= 10; i++) 
{
    std::cout << "Image" << i+1 << ".png";
}
Sign up to request clarification or add additional context in comments.

Comments

2

You don't; you use an integer variable, and make a string representation of it to build the file name: std::to_string(n)

Comments

1
int value = atoi(postFix.c_str());
value++;
postFix = std::to_string(value);

2 Comments

Did you mean int value = std::stoi(postFix);? en.cppreference.com/w/cpp/string/basic_string/stol
Nope, i meant exactly what i put :), but yours works equally as well!
1

use std::to_string

for (int i = 0; i <= 10; i++) 
{
    int postFix = 1;
    std::cout << (std::string("Image" + std::to_string(postFix) + std::string(".png")));
}

Comments

1

It seems you're getting it more complicated than necessary.

for (int i = 1; i <= 10; i++) {
     std::cout << "Image" << i << ".png";
   }

or

for (int i = 1; i  <= 10; i++) {
    std::string filename = "Image" + std::to_string(i) + ".png";
    // do something with filename
}

to_string(int) appeared in C++11.

Comments

1

Write simply

std::cout << std::string("Image" ) + std::to_string( i + 1 ) + ".png";

Or even like

std::cout << "Image" + std::to_string( i + 1 ) + ".png";

Or you can define at first a string

std:;string = "Image" + std::to_string( i + 1 ) + ".png";

and then output it

std::cout << s;

Comments

0

Increment the int 'Number' as your wish, then convert it into string and finally append it with the file name (or exactly where you want).

Headers needed:

#include <iomanip>
#include <locale>
#include <sstream>
#include <string> // this should be already included in <sstream>

And to convert the 'Number' to 'String' do the following:

int Number = 123;       // number to be converted to a string

string Result;          // string which will contain the result

ostringstream convert;   // stream used for the conversion

convert << Number;      // insert the textual representation of 'Number' in the characters in the stream

Result = convert.str(); // set 'Result' to the contents of the stream

          // 'Result' now is equal to "123" 

This operation can be shorten into a single line:

int Number = 123;
string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();

Reference taken from here.

2 Comments

Erh - did you post your answer to the wrong question?
@kfsone, Im in the middle of editing

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.