2

私は 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] = '\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);
}
4

1 に答える 1

2

私に際立っている最初の問題はこれです:

filename[size] = '\0';

sizeは無効なインデックスです。これは、文字filenameのみを指してsizeいる ( にインデックス付けされ0ているsize-1) ためです。strcpyヌル文字がコピーされるため、その行は必要ありません。についても同じですcdt[size] = '\0'strftimenull 文字が追加されます。

于 2013-08-06T11:13:33.163 に答える