CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    4

    Red face 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

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    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.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    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.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    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

  5. #5
    Join Date
    Dec 2009
    Posts
    145

    Re: converting integer to string and appending

    All links are pointing to only 1 solution. Please suggest another link for OP to learn more

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: converting integer to string and appending

    Quote Originally Posted by Ledidas View Post
    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

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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"?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Feb 2005
    Posts
    2,160

    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...
      }

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: converting integer to string and appending

    Quote Originally Posted by laserlight View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured