Hi,

I want to create a text file with the name as SYSTEM DATE...
for example---28/04/2008:9:11:50.txt
The File name should be allocated dynamically.

Can you provide the c code for this?

Plese help me.

You can not use / characters in file name. The best format is YYYYMMDD (Year Month Day)format because such file names can be easily sorted sorrecty.

To get the system time use the localtime() function found in time.h. That will return a structure that you can use either sprintf() or strftime() to format the time into a character array.

Edited 5 Years Ago by Ancient Dragon: n/a

Thanks for ur reply.I used strftime to get the date.
I am not getting how to open a text file with this and i need to append the data from mouse into this file

This is my code:

#include <time.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/types.h>
#include<fcntl.h>
int main()
{
char s[30];
int fp;
size_t i;
struct tm tim;
time_t now;
now = time(NULL);
tim = *(localtime(&now));
i = strftime(s,30,"%b %d, %Y; %H:%M:%S\n",&tim);
printf("%s\n",s);
}

s contains Dec 22, 2010; 18:45:21.txt
I tried fopen.but the file is not opening
i = strftime(s,30,"%b %d, %Y; %H:%M:%S.txt",&tim);
fp=fopen("s","w");

i can't able to see the file Dec 22, 2010; 18:45:21.txt in the directory when i type ls.

This article has been dead for over six months. Start a new discussion instead.