GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Apr-2009, 16:32
Kufu Kufu is offline
New Member
 
Join Date: Sep 2005
Posts: 10
Kufu is on a distinguished road

Convert an int to a string?


Hello. I'm needing to loop through files in a directory using the number in their file name. I've looked everywhere, but for the life of me I can't find a way to convert an int into a string. If I could find a way, I'm going to just stick the counter for my loop into the string I send to the function that opens the files.

This can't be all that uncommon a task, so I figured there would have to be a way built into C++ or the string library, but I can't seem to find it.
  #2  
Old 23-Apr-2009, 17:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Convert an int to a string?


Quote:
Originally Posted by Kufu
.number in their file name....

You might consider using a stringstream to convert an int to a std::string, along with whatever other stuff makes up the file name. Then the std::string c_str() member function can be used as an argument to open an ifstream or an ofstream.

For example:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    string prefix = "foo";
    string suffix = "bar.txt";

    for (int i = 0; i < 10; i++) {
        stringstream ss; // An empty stringstream
        ss << prefix << i << suffix;

        cout << "i = " << i << ": name = " << ss.str() << endl; //ss.str() is a C++ std::string

        ofstream outstream(ss.str().c_str()); // File name is a C-style "string" (pointer to char)
        if (!outstream) {
            cerr << "Can't open file " << ss.str() << " for writing" << endl;
            exit(EXIT_FAILURE);
        }
        //
        // Do whatever you want with this file.
        // Then close it so that outstream can be reused the next time through the loop.
        //
        outstream.close();
    }
    return 0;
}

Output:

Code:
i = 0: name = foo0bar.txt i = 1: name = foo1bar.txt i = 2: name = foo2bar.txt i = 3: name = foo3bar.txt i = 4: name = foo4bar.txt i = 5: name = foo5bar.txt i = 6: name = foo6bar.txt i = 7: name = foo7bar.txt i = 8: name = foo8bar.txt i = 9: name = foo9bar.txt

See stringstream - C++ Reference

Regards,

Dave
  #3  
Old 23-Apr-2009, 19:45
Kufu Kufu is offline
New Member
 
Join Date: Sep 2005
Posts: 10
Kufu is on a distinguished road

Re: Convert an int to a string?


Oh wow, that things pretty neat. And it worked. Thanks a bunch ^^
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Equation to create a Tax Liability info output ThatDudeDave Java Forum 2 10-Dec-2008 07:53
convert string to double and double to string vard3000 MS Visual C++ / MFC Forum 2 01-Feb-2007 20:10
Message Class TransformedBG C++ Forum 5 29-Nov-2006 22:28
Help wit my source code compiler errors Krandygrl00 C++ Forum 1 06-Jun-2005 09:14
Convert Unsigned short int to a string... Pissus C++ Forum 2 11-Jan-2005 09:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:58.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.