How to write the filename with date and time as Filename

I am trying to generate some files depends on the time of creation (execution)

I got the time stamp as example as the following:

/* ctime example */
#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime;

time ( &rawtime );
printf ( "The current local time is: %s", ctime (&rawtime) );

return 0;
}

And I have written a simple program that will generate a file
//include this file for cout
#include <iostream.h>
#include <stdio.h>
#include <time.h>

using namespace std;

int main() {
FILE *file=NULL;

time_t rawtime;
time ( &rawtime );

file = fopen ( "TESTOUT.txt", "w"); // how to name my file with date and time
fprintf (file, "ABCDEFGHIJK");
return 0;

}

My problem is, how to name my file with date and time like:

TESTOUT001.txt
TESTOUT002.txt
TESTOUT003.txt
TESTOUT004.txt
TESTOUT005.txt
etc

or
TESTOUT04-Jun-08.txt
TESTOUT05-Jun-08.txt
TESTOUT06-Jun-08.txt
etc

Most idealy would be 001->> 999 or Time as name for me?

Please let me know how to combine those 2 piece of code together?

thank you


Last edited on
To get the system time as a string...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <time.h"
#include <string>

#define MAX_DATE 12

std::string get_date(void)
{
   time_t now;
   char the_date[MAX_DATE];

   the_date[0] = '\0';

   now = time(NULL);

   if (now != -1)
   {
      strftime(the_date, MAX_DATE, "%d_%m_%Y", gmtime(&now));
   }

   return std::string(the_date);
}


You may want to change the formatting ("%d_%m_%Y") just check the man page for strftime() for the various formats.
Thanks for the info,

Does it mean i need to put my XXX.txt name as string?

Because I am aiming to generate my txt file like the following

//time day month year.txt
1600-04Jun08.txt
1601-04Jun08.txt
1602-04Jun08.txt
1603-04Jun08.txt

Anyone know how to combine them?

Can I add like:

strftime+ ".txt" ??

right?
Actually you can put the ".txt" right in the strftime() format string.
Topic archived. No new replies allowed.