Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a filename with basepath + extension and current date in C [char *array]
    text
    copied!<p>I'm a totally newbie in C but I want to create a string (not C++ string) with a basepath, current date and time and a given extension. I saw this topic: <a href="https://stackoverflow.com/questions/5138913/c-putting-current-date-in-a-filename">C - putting current date in a filename</a> and created my simple code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;time.h&gt; char* currdatetime() { const int size = 20; char *cdt = (char*)malloc(sizeof(char)*size); if(cdt == NULL) { return NULL; } memset (cdt, 0, size); time_t currDateTime; currDateTime = time(NULL); if(currDateTime == -1) { return NULL; } struct tm *timeinfo = localtime (&amp;currDateTime); if(strftime(cdt, 20, "%d.%m.%y_%H:%M:%S", timeinfo) == 0) { return NULL; } cdt[size] = '\0'; return cdt; } char *getname(const char *pathtofile, const char *ext) { char *timestamp = currdatetime(); int size = (strlen(pathtofile) + strlen(ext) + strlen(timestamp) + 1); char *filename = (char*)malloc(sizeof(char)*size); if(filename == NULL) { return NULL; } memset (filename, 0, size); strcpy(filename, pathtofile); strcpy(filename+strlen(pathtofile), timestamp); strcpy(filename+strlen(pathtofile)+strlen(timestamp), ext); filename[size] = '\0'; return filename; } int main(void) { printf("\n\n%s\n\n", getname("file_", ".txt")); printf("\n\n%s\n\n", getname("file_", ".html")); return(0); } </code></pre> <p>But the output is like this:</p> <pre><code>file_06.08.13_13:06:47.txt *** glibc detected *** ./test: free(): invalid pointer: 0x092f6020 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7630ee2] /lib/i386-linux-gnu/libc.so.6(+0xa96f9)[0xb76646f9] /lib/i386-linux-gnu/libc.so.6(+0xa99f2)[0xb76649f2] /lib/i386-linux-gnu/libc.so.6(localtime+0x2d)[0xb766311d] ./test[0x8048572] ./test[0x80485be] ./test[0x804874a] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75d44d3] ./test[0x8048471] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 08:07 663115 /home/ivy/Desktop/CTests/test 08049000-0804a000 r--p 00000000 08:07 663115 /home/ivy/Desktop/CTests/test 0804a000-0804b000 rw-p 00001000 08:07 663115 /home/ivy/Desktop/CTests/test 092f6000-09317000 rw-p 00000000 00:00 0 [heap] b757f000-b759b000 r-xp 00000000 08:05 656309 /lib/i386-linux-gnu/libgcc_s.so.1 b759b000-b759c000 r--p 0001b000 08:05 656309 /lib/i386-linux-gnu/libgcc_s.so.1 b759c000-b759d000 rw-p 0001c000 08:05 656309 /lib/i386-linux-gnu/libgcc_s.so.1 b75ba000-b75bb000 rw-p 00000000 00:00 0 b75bb000-b775e000 r-xp 00000000 08:05 655398 /lib/i386-linux-gnu/libc-2.15.so b775e000-b7760000 r--p 001a3000 08:05 655398 /lib/i386-linux-gnu/libc-2.15.so b7760000-b7761000 rw-p 001a5000 08:05 655398 /lib/i386-linux-gnu/libc-2.15.so b7761000-b7764000 rw-p 00000000 00:00 0 b777f000-b7783000 rw-p 00000000 00:00 0 b7783000-b7784000 r-xp 00000000 00:00 0 [vdso] b7784000-b77a4000 r-xp 00000000 08:05 659665 /lib/i386-linux-gnu/ld-2.15.so b77a4000-b77a5000 r--p 0001f000 08:05 659665 /lib/i386-linux-gnu/ld-2.15.so b77a5000-b77a6000 rw-p 00020000 08:05 659665 /lib/i386-linux-gnu/ld-2.15.so bfefc000-bff1d000 rw-p 00000000 00:00 0 [stack] Przerwane (core dumped) file_06.08.13_13:06:47.txt *** glibc detected *** ./test: free(): invalid pointer: 0x092f6020 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7630ee2] /lib/i386-linux-gnu/libc.so.6(+0xa96f9)[0xb76646f9] /lib/i386-linux-gnu/libc.so.6(+0xa99f2)[0xb76649f2] /lib/i386-linux-gnu/libc.so.6(localtime+0x2d)[0xb766311d] ./test[0x8048572] ./test[0x80485be] ./test[0x804874a] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75d44d3] ./test[0x8048471] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 08:07 663115 /home/ivy/Desktop/CTests/test 08049000-0804a000 r--p 00000000 08:07 663115 /home/ivy/Desktop/CTests/test 0804a000-0804b000 rw-p 00001000 08:07 663115 /home/ivy/Desktop/CTests/test 092f6000-09317000 rw-p 00000000 00:00 0 [heap] b757f000-b759b000 r-xp 00000000 08:05 656309 /lib/i386-linux-gnu/libgcc_s.so.1 b759b000-b759c000 r--p 0001b000 08:05 656309 /lib/i386-linux-gnu/libgcc_s.so.1 b759c000-b759d000 rw-p 0001c000 08:05 656309 /lib/i386-linux-gnu/libgcc_s.so.1 b75ba000-b75bb000 rw-p 00000000 00:00 0 b75bb000-b775e000 r-xp 00000000 08:05 655398 /lib/i386-linux-gnu/libc-2.15.so b775e000-b7760000 r--p 001a3000 08:05 655398 /lib/i386-linux-gnu/libc-2.15.so b7760000-b7761000 rw-p 001a5000 08:05 655398 /lib/i386-linux-gnu/libc-2.15.so b7761000-b7764000 rw-p 00000000 00:00 0 b777f000-b7783000 rw-p 00000000 00:00 0 b7783000-b7784000 r-xp 00000000 00:00 0 [vdso] b7784000-b77a4000 r-xp 00000000 08:05 659665 /lib/i386-linux-gnu/ld-2.15.so b77a4000-b77a5000 r--p 0001f000 08:05 659665 /lib/i386-linux-gnu/ld-2.15.so b77a5000-b77a6000 rw-p 00020000 08:05 659665 /lib/i386-linux-gnu/ld-2.15.so bfefc000-bff1d000 rw-p 00000000 00:00 0 [stack] (core dumped) </code></pre> <p>And I dont really know what is wrong and how to fix this ...</p> <p><strong>OK, added this, result is the same:</strong></p> <pre><code>char *f1 = getname("file_", ".txt"); char *f2 = getname("file_", ".html"); printf("\n\n%s\n\n", f1); printf("\n\n%s\n\n", f2); free(f1); free(f2); </code></pre> <p><strong>CODE THAT FINALLY WORKED:</strong></p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; #include &lt;time.h&gt; char* currdatetime() { const int size = 20; char *cdt = (char*)malloc(sizeof(char)*size); if(cdt == NULL) { return NULL; } memset (cdt, 0, size); time_t currDateTime; currDateTime = time(NULL); if(currDateTime == -1) { return NULL; } struct tm *timeinfo = localtime (&amp;currDateTime); if(strftime(cdt, 20, "%d.%m.%y_%H:%M:%S", timeinfo) == 0) { return NULL; } return cdt; } char *getname(const char *pathtofile, const char *ext) { char *timestamp = currdatetime(); int size = (strlen(pathtofile) + strlen(ext) + strlen(timestamp) + 1); char *filename = (char*)malloc(sizeof(char)*size); if(filename == NULL) { return NULL; } memset (filename, 0, size); strcpy(filename, pathtofile); strcpy(filename+strlen(pathtofile), timestamp); strcpy(filename+strlen(pathtofile)+strlen(timestamp), ext); free(timestamp); timestamp = NULL; return filename; } int main(void) { char *f1 = getname("file_", ".txt"); char *f2 = getname("file_", ".html"); printf("\n\n%s\n\n", f1); printf("\n\n%s\n\n", f2); free(f1); free(f2); return(0); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload