在C[char*array]中创建一个具有basepath+扩展名和当前日期的文件名



我是一个C语言的新手,但我想创建一个具有基本路径、当前日期和时间以及给定扩展名的字符串(而不是C++字符串)。我看到了这个主题:C-将当前日期放在文件名中,并创建了我的简单代码:

#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] = '';
    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] = '';
    return filename;
}
int main(void)
{
    printf("nn%snn", getname("file_", ".txt"));
    printf("nn%snn", 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)

我真的不知道出了什么问题,也不知道如何解决。。。

好的,加上这个,结果是一样的:

char *f1 = getname("file_", ".txt");
char *f2 = getname("file_", ".html");
printf("nn%snn", f1);
printf("nn%snn", 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("nn%snn", f1);
    printf("nn%snn", f2);
    free(f1);
    free(f2);
    return(0);
}

对我来说最突出的第一个问题是:

filename[size] = '';

size是无效索引,因为filename仅指向size字符(索引为0size-1)。您不需要这一行,因为strcpy会为您复制空字符。cdt[size] = ''也是如此;strftime将为您添加一个空字符。

相关内容

  • 没有找到相关文章

最新更新