リンクされたリストとして構造体を使用していますが、最近の変更 (git リポジトリにチェックインするのを忘れたので、どの変更か覚えていません) 以来、head 要素の構造体変数の 1 つが変更されています。以下に示すコードの実行中、post->filename は有効な文字列を取得しましたが、メソッドを終了した後、head_post->filename (まったく同じ値を指す必要があります) にゴミが追加されています。文字列「20130804-0638.md」は「20130804-0638.md�:\020」になります。
私が見逃しているものはありますか?
構造:
struct posting {
char *filename;
char timestamp[17];
char *url;
char *title;
char *html;
struct posting *next;
};
struct posting *head_post = NULL;
コード:
struct posting *post;
...
while ((ep = readdir(dp))) {
if (ep->d_name[0] != '.' && strstr(ep->d_name, ".md") && strlen(ep->d_name) == 16) {
if (head_post == NULL) {
head_post = malloc(sizeof(struct posting));
post = head_post;
} else {
post = head_post;
while (post->next != NULL)
post = post->next;
post->next = malloc(sizeof(struct posting));
post = post->next;
}
post->filename = malloc(sizeof(char) * strlen(ep->d_name));
strcpy(post->filename, ep->d_name);
post->next = NULL;
}
}