converting integer to string and appending
hi all,
i want to convert a number (in a for loop) and append that to "_s.jpg"
i.e. as the for loop keeps going my string should be:
"1_s.jpg"
"2_s.jpg"
"3_s.jpg"
etc.
so any functions i could use to achieve this?
thanks in advance for any help
sachin
Re: converting integer to string and appending
To convert an int to a string. As for appending a string to another string, that's just string concatenation.
Re: converting integer to string and appending
Hi
you can follow up on the link below
Q: How to convert a string into a numeric type?
Edit:
My apology!
I thought it was the other way around.
Re: converting integer to string and appending
Re: converting integer to string and appending
All links are pointing to only 1 solution. Please suggest another link for OP to learn more
Re: converting integer to string and appending
Quote:
Originally Posted by
Ledidas
All links are pointing to only 1 solution.
It's the solution that any experienced C++ coder would use.
Unless the OP wants a 'C' solution.
Re: converting integer to string and appending
Quote:
Originally Posted by Ledidas
All links are pointing to only 1 solution. Please suggest another link for OP to learn more
Boost.Format would be an alternative, but it may end up using a stringstream under the hood, and is probably overkill when you just want to prepend a number.
Anyway, why don't you "suggest another link for OP to learn more"?
Re: converting integer to string and appending
I think sprintf is perfectly acceptable in this case. It's a filename so a char buffer of MAX_PATH (assuming microsoft) is applicable and probably the simplest approach:
Code:
char filename[MAX_PATH];
for(int i=0;i<10;i++){
sprintf(filename,"%d_s.jpg",i);
//do whatever with filename...
}
Re: converting integer to string and appending
Quote:
Originally Posted by hoxsiew
It's a filename so a char buffer of MAX_PATH (assuming microsoft) is applicable
Given that one will be prepending a number, it can be even shorter.
Re: converting integer to string and appending
Quote:
Originally Posted by
laserlight
Boost.Format would be an alternative, but it may end up using a stringstream under the hood, and is probably overkill when you just want to prepend a number.
Anyway, why don't
you "suggest another link for OP to learn more"?
Boost lexical_cast could be a good solution.
But I would never recommend to somebody using boost, if they don't understand how it is done without.