Thread: Sequential O.F. naming at runtime

  1. #1
    Registered User
    Join Date
    Oct 2002

    Question Sequential O.F. naming at runtime

    I apologize in advance if this is an old question, but I have not been able to uncover it in my searches.

    I have written a program which simulates plant population dynamics in a spatial grid. I would like it to output annual population statistics; however, I would like the program to be felxible enough to determine the output filenames at run time by concatenating:
    a) a user-defined base string (e.g. "year")
    b) the integer value of time from the program's internal clock (e.g. 1)
    c) a predefined string representing the appropriate output file extension (e.g. ".txt")

    In this example the resulting filename, determined during the run, would be "year1.txt".

    I can not figure out how to convert the value of the integer variable "Time" into a string variable so that I can easily combine it with the other string variables. Text and web searches have come up blank. I hope someone can help me out, and thank you in advance for your advice!

  2. #2
    I lurk
    Join Date
    Aug 2002
    Here's an example of how you could do it:

    PHP Code:
    #include <sstream>
    #include <iostream>
    //#include <string>
    #include <ctime>

    int main()
    using namespace std;

    stringstream filename;
    time_t rawTime time(NULL);
    tm *currentTime localtime(&rawTime);

    filename << "year" << currentTime->tm_year 1900 << ".txt";
    // filename now containts "year2002.txt"

    cout << filename.str().c_str();
    // output it
    return 0;
    Last edited by Eibro; 10-21-2002 at 03:23 PM.

  3. #3
    Join Date
    Feb 2002
    //make sure to include
    #include <stdlib.h>
    //let time be your integer amount for filename
    char tempbuf[6];
    string yearNum;
    yearNum = tempbuf;
    Then all you have to do is concatenate the rest of your filename together.

  4. #4
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Using sprintf makes formatting of the filename a lot easier :
    int year = 2002;
    char filename[256];
    sprintf(filename, "%4d.txt", year);

  5. #5
    Registered User
    Join Date
    Oct 2002

    Talking Thanks so much!

    You folks rock. Thanks for all the suggestions; I had seen itoa but could not quite figure out the correct usage. The other options look great as well (is sprintf in the std library?)

    Thanks so much for your help! I'll post a reply to let you know what worked for me.

  6. #6
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    I think this was asked yesterday....some classes homework? hehe

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  7. #7
    Registered User
    Join Date
    Oct 2002

    Unfortunately, no...

    I wish I could take a class in this... I'm just an ecologist trying to pick up enough C++ programming to get what I need done.

    I couldn't find an instance of this asked before, but apparently I didn't look hard enough; sorry to clutter the board with a repeat question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. link with C runtime library
    By George2 in forum C++ Programming
    Replies: 26
    Last Post: 02-05-2008, 01:56 AM
  3. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM