Title: C create txt file and append current date and time as a name
Tags: c;file-io
Question: I did not manage to find how can I create txt file in C , for example "test.txt" and then to append to the file name current date and time? Every time when program runs it will create test.txt but with different date and time?  this. For now I'm creating txt file simply using open function with flags, but I'm not sure how to append date and time ?
fd = open("test.txt", O_RDWR | O_CREAT | O_APPEND, 0777);
Comment: You'll want to [create the text file](http://www.informatika.unsyiah.ac.id/arie/algoProC/learnProgZaNet/programming_c_learn10.html) using the string representation of [the current time](http://www.cplusplus.com/reference/ctime/localtime/) as the name.

Comment: Please add your current code to your question

Here is the accepted answer: I'm not sure of what your code does, so here is the most basic implementation for your problem. I think this can help you.
There are more elegant ways to solve it, but I think you should start from the bottom and work your way up
FILE *fp;
// insert the date into the char array
char text[17];
time_t now = time(NULL);
struct tm *t = localtime(&amp;now);
strftime(text, sizeof(text)-1, "%dd %mm %YYYY %HH:%MM", t);
text[16] = 0;
// concat the date to file name
char *filename;
if((filename = malloc(strlen("C:\\Temp\\filename.txt")+strlen(text)+1)) != NULL){
filename[0] = '\0';   // ensures the memory is an empty string
strcat(filename,"C:\\Temp\\filename");
strcat(filename,text);
strcat(filename,".txt");
// use the file
fp = fopen(filename, "w+");
fputs("This is testing for fputs...\n", fp);
fclose(fp);
Hope it helps
Comment for this answer: This is exactly that I wanted. One more question, how can I change FILE *fp to point to another directory ?
Comment for this answer: I tried but problem is that if you want to add path you must specifies at the end filename.txt with extenstion, you can not add filename as a char * filename which we have in the example above
Comment for this answer: Well, I thought to make something more simple , for example :     char server_table[256] = {0};
sprintf(server_table,"%s%s%d%s%f%s%s%s","server_table","_",buffer_size,"_",delta_time,"_",ctime(&mytime),".txt");
FILE *test = "C://Users//john//Documents//Results";
test = fopen(server_table,"wb"); but I have problem with FILE *test to point to another directory
Comment for this answer: Ok, I won't complicated. I will adopt to your way because its working. Just I'm worried about malloc, should I free it somewhere? Because I will have 4 files to create on this way
Comment for this answer: Also problem with your solution is that I want to add some integers in the filename before date, but I can't do this using strcat
Comment for this answer: In order to set the directory of the file pointer (fp), you should add the path to the filename.
meaning: instead of "filename.txt" use "C:\\Temp\\filename.txt"

Here is another answer: You may use ctime.h library functions.
time_t time(time_t *) returns the current time to you.
To convert it into a formatted time string, consider using struct tm * gmtime(time_t *).
Then use int sprintf(char *, const char * ...) to format your file name string.
Spend some time on reading these library documents:
time (http://www.cplusplus.com/reference/ctime/time/)
gmtime (http://www.cplusplus.com/reference/ctime/gmtime/)
sprintf (http://www.cplusplus.com/reference/cstdio/sprintf/?kw=sprintf)

Here is another answer: Using predefined ctime function:
#include <time.h&gt;
#include <stdlib.h&gt;
#include <stdio.h&gt;
int main(int argc, char* argv[])
time_t current_time;
FILE* file;
current_time = time(NULL);
file = fopen("test.txt", "w+");
fprintf(file, "%s", ctime(&amp;current_time));
return 0;
Using strftime function:
#include <stdio.h&gt;
#include <time.h&gt;
int main ()
time_t rawtime;
struct tm* timeinfo;
char buffer[80];
FILE* file;
file = fopen("test.txt", "w+");
time(&amp;rawtime);
timeinfo = localtime(&amp;rawtime);
strftime(buffer, 80, "%I:%M:%S", timeinfo);
fprintf(file, "%s", buffer);
return 0;

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:08/21/14', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c><file-io>', 'Title': 'C create txt file and append current date and time as a name', 'ViewCount': 6882, 'id': 25420933, 'user_AboutMe': None, 'user_AccountId': 4766905.0, 'user_DisplayName': 'user3852803', 'user_Reputation': 144.0, 'user_Views': 70.0, 'user_id': 3852803.0}}