Thread: Converting an integer into a string

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Converting an integer into a string

    Hello, I am trying to save a number of files with the filename format: "image#.bmp", where # is some number between 0 and 1024. I do not know how to get the number in there in a generic way, though.

    I know that there is a strtol routine in C, is there anything like the reverse, some simple way of converting an integer (like 72, 5551, 132) to a string? Using either C routines or (preferably) the C++ String class.

    Thank you in advance. I'm sure this is a somewhat common question.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I prefer to use sprintf() to do this, e.g.
    [code
    sprintf(filename, "image%04d.bmp", imgno);
    [/code]

    Using %04d will make the number look like 0001, 0012, 0123 or 1234 - so all the files come in numerical order if you list them by name (otherwise image9.bmp gets way down past image1xxx, image2xx, etc).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You can use stringstreams to do this which is basically an object-oriented (C++) way of doing the same thing as the sprintf function.

    Code:
    #include <sstream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    using namespace std;
    
    ...
    
    for( int i = 1; i <= 1024; ++i )
    {
        stringstream sstr;
        sstr << "image" << setw(4) << setfill('0') << i << ".bmp";    // sstr = "image#.bmp" where # is [1..1024]
        ofstream out(sstr.str().c_str());                             // Open file "image#.bmp" for writing
        ...
        // Do stuff with the file here
        ...
        out.close();
    }
    Same provisions provided above that matsp mentioned about formating the numerical values as 0001, 0002, etc...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Thank you both - I've got it solved (I used the sprintf version, but I'll move to stream version soon).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Converting a string to an integer
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2001, 10:01 PM