我是C中的新手,但我想创建一个带有基本路径,当前日期和时间以及给定扩展名的字符串(不是C ++字符串)。我看到了这个主题:C - putting current date in a filename并创建了我的简单代码:
#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)
我真的不知道出了什么问题以及如何解决这个问题......
好的,添加了这个,结果是一样的:
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)
第一个突出问题的问题是:
filename[size] = '\0';
size
是无效索引,因为filename
仅指向size
个字符(已编入索引0
至size-1
)。您不需要该行,因为strcpy
会为您复制空字符。 cdt[size] = '\0'
也是如此; strftime
将为您添加空字符。