我在C语言中进行一些文本提取,并在我的循环中调用 免费时得到一些“垃圾字符串”。下面是一些示例文本:C免费垃圾字符指针
Sentence #1 (34 tokens):
The Project Gutenberg EBook of Moby Dick; or The Whale, by Herman Melville
This eBook is for the use of anyone anywhere at no cost and with
almost no restrictions whatsoever.
[Text=The CharacterOffsetBegin=0 CharacterOffsetEnd=3 PartOfSpeech=DT Lemma=the] [Text=Project CharacterOffsetBegin=4 CharacterOffsetEnd=11 PartOfSpeech=NN Lemma=project]
问:
1 - 我可以放心地重用指针变量自由之后?
感谢您的帮助!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_M (1024*100)
int main(int argc, char **argv)
{
FILE *file;
char buff[LINE_M];
char *lemma;
char *text;
char *sentence = NULL;
char *p, *t;
int numSent, numTok;
file = fopen("moby.txt.out", "r");
while (fgets(buff, LINE_M, file))
{
if(sscanf(buff, "Sentence #%d (%d tokens):", &numSent, &numTok))
continue;
if(strstr(buff, "[Text=") == NULL)
{
if(sentence != NULL)
{
sentence = realloc(sentence, (strlen(buff) + strlen(sentence) + 2) * sizeof(char));
strcat(sentence, buff);
}
else
{
sentence = malloc(sizeof(char) * (strlen(buff) + 1));
strcpy(sentence, buff);
}
continue;
}
p = buff;
while ((p = strstr(p, "Text=")) != NULL)
{
p += 5;
t = strchr(p, ' ');
text = malloc((int)(t - p));
strncpy(text, p, (int)(t - p));
p = strstr(t + 1, "Lemma=") + 6;
t = strchr(p, ']');
lemma = malloc((int)(t - p) * sizeof(char));
strncpy(lemma, p, (int)(t - p));
p = t + 1;
printf("%s\n", lemma);
free(text);
free(lemma);
text = NULL;
lemma = NULL;
}
free(sentence);
sentence = NULL;
}
fclose(file);
return 0;
}
重新使用释放指针指向的内存是不行的,但将新指针存储在存储您释放的旧内存的同一个内存槽中是完全正确的。 (我很确定你问的是第二种情况。) – DaoWen