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.

Recommended Answers

All 6 Replies

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.

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);
}

Does s contain the file name you want to open? If so, use it in your fopen() statement.

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.

you formatted the fopen() incorrectly. Do not put s in quotes fp = fopen(s,"w");

thankyou very much its working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.