Thread: creating a filename based on a variable

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    Question creating a filename based on a variable

    I'm writing some code that will create an XML file based on a logged in users id number ie: 04390 will create 04390.xml and so forth. I have some code like this...
    Code:
    int studentID;
    
    
    yada yada yada
    
    ofstream fout("//ID BASED FILE NAME HERE")
    how can i control what will go in that commented area marked "id based file name here"?
    i don't want to hard code in a lot of
    Code:
    switch (studentID) {
                case 04390:
                         ofstream fout("04390.xml");
                         break;
                }
    so does anybody have any bright ideas on how do accomplish my goal?? if anyone can help it would be greatly appreciated. thanks
    PHP and XML
    Let's talk about SAX

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Create a variable to hold the filename. I presume you have access to the username somewhere, so just copy it into the filename variable. Then concatenate the extension onto the end.

    Then just pass the filename variable into the functions where needed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Code:
    	
    #include <sstream>
    #include <string>
    #include <fstream>
    /** Converts any type T to string if it has operator << (std::ostream&, const T&) defined */
    template <class T>	std::string toString(const T& val) {
    		std::ostringstream strWriter;
    		strWriter << val;
    		return strWriter.str();
    }
    
    int main() {
       int studentID;
    // stuff
       std::string filename = toString<int>(studentID) + ".xml";
       std::ofstream file(filename.c_str());
    // write to file
        return 0;
    }
    edit: Added C string conversion in filename constructor.. sorry :(.
    Last edited by SilentStrike; 05-22-2002 at 09:40 PM.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    um...

    save the files name as a char* and then pass that variable into the open(char* filename); funciton in the wonderful fstream.h library.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. creating counters and code not working
    By geekrockergal in forum C Programming
    Replies: 2
    Last Post: 02-05-2009, 12:50 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM