How to create dynamic File name using C?

Hi Friends..

I've problem regarding how to create dynamic file using C.
I need to create Log Files for machine.
I think i want to design the Log File names looks like as follows :
20101222.txt (YYYY-MM-DD)

So, every days my program (DLL) will create a Log Files for each days.
1 day 1 log file, and note all information happened at that day.

For create File the code is used as follows :

char * FILE_NAME = "C:\\Test\\20101222.txt"); //How to change this to current date?
FILE *Ifp;
fp = fopen(FILE_NAME, "a");
-- do writing information --
-- do writing information --
-- do writing information --
fclose(fp);

Please help me regarding this.

Thanks.
Hi

Heres an example to show YYYYMMDD it should be easy to convert to a filename

Notice how you have to add 1900 to the year & 1 to the month.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
#include <time.h>

int	main(int argc,char* argv)
{
		time_t	timeIn;
		tm*		dateTime;
		char*	tempString = new char[32];

		time(&timeIn);

		dateTime = localtime(&timeIn);

		sprintf_s(tempString,32,"%d%d%d",1900 + dateTime->tm_year,
						1+dateTime->tm_mon,dateTime->tm_mday);

		printf("%s",tempString);


}


Hope this helps
Shredded
Topic archived. No new replies allowed.