2013-08-06 14 views
2

我在C中是一个全新手,但我想创建一个带有basepath的字符串(不是C++字符串),当前日期和时间以及给定的扩展。我看到这个题目:C - putting current date in a filename和创建了简单的代码:在C中创建一个带有basepath +扩展名和当前日期的文件名[char * array]

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <time.h> 

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

但输出是这样的:

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) 

而且我真的不知道什么是错的,如何解决这一问题?

OK,添加了这个,结果是一样的:

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

代码不终于摸索:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <time.h> 

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 (&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); 
} 
+0

你使用调试器吗? –

+2

这是令人印象深刻的,考虑到**你不会在你的代码**中的任何地方调用'free'。 (向内存泄漏问好!) – nneonneo

+0

'malloc + memset(0)= calloc' –

回答

2

脱颖而出对我的第一个问题是这样的:

filename[size] = '\0'; 

size是一个无效的索引,因为filename仅指向size字符(索引0size-1)。您不需要该行,因为strcpy会为您复制空字符。 cdt[size] = '\0'同样的事情; strftime会为你添加一个空字符。

+0

谢谢!它现在的作品:http://pastie.org/private/nc4asjcyh6gfh8sj9i7lna – ivy

+1

很高兴我能帮上忙。 (哦,并确保释放'timestamp'!) – nneonneo

+0

再次感谢,做到了:http://pastie.org/private/0jvticswg6o9r5ttlimma我在分配和释放C中的内存和C中的字符串时遇到了很多麻烦:/ – ivy

相关问题