|
-
February 1st, 2010, 03:46 AM
#1
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
-
February 1st, 2010, 04:02 AM
#2
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.
-
February 1st, 2010, 04:02 AM
#3
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.
Last edited by potatoCode; February 1st, 2010 at 05:55 AM.
-
February 1st, 2010, 04:03 AM
#4
Re: converting integer to string and appending
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 1st, 2010, 06:14 AM
#5
Re: converting integer to string and appending
All links are pointing to only 1 solution. Please suggest another link for OP to learn more
-
February 1st, 2010, 06:23 AM
#6
Re: converting integer to string and appending
 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.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
February 1st, 2010, 09:26 AM
#7
Re: converting integer to string and appending
 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"?
-
February 1st, 2010, 10:05 AM
#8
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...
}
-
February 1st, 2010, 10:07 AM
#9
Re: converting integer to string and appending
 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.
-
February 1st, 2010, 10:25 AM
#10
Re: converting integer to string and appending
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|