好吧,所以我有这个学校项目,我有一个很大的问题。我花了大约3-4个小时进行调试,但我不知道为什么会发生这种情况。
该程序这样做:它从文件"input.in"中读取以下内容:N(<500),然后是N个文件名,M(<500)和M个"查询"。查询是这样一行:"John&Papa|Dan",它将返回包含"John and Papa"或"Dan"的文件索引。
该算法有效,我认为问题在于保存哈希表。在小测试中,程序运行良好,然后我进行了110个文件的测试,它只是"分段错误"。
到目前为止,我所知道的是它的分割错误:
- 它在第9个文件出现故障
- 它在哈希表中还不存在的单词处出错
- 它在搜索匹配时通过所有列表后出错(在列表末尾,添加新值之前)
这是代码:http://pastebin.com/fd4c1f6w
此外,标题:http://pastebin.com/H0m7WjrG
以下是调试信息:http://pastebin.com/gvvyjePZ
输入文件:http://www.sendspace.com/file/48etji
求你了,我真的需要解决这个问题,而且我真的很失望。
我的评论会有点长,所以我会把它发布在这里。在图形调试器如此容易出现之前,我们使用了一种称为"tombstone调试"的技术。基本上,您只需在代码中添加一些printf语句,就可以确定程序执行的最后一个位置。printf("(%d) %sn", __LINE__, __FILE__);
对此非常有用,顺便说一下
我对您的代码进行了这样的处理,这是一种快速隔离需要更仔细查看的代码的方法。我发现只读取了第一个文件"date.in"。然后我发现对put_doc的第二个调用没有返回。
然后我意识到你已经发布了Alocare_Mapare和Realocare_Mapare的更新代码。:手掌:
当我用您的新函数更新代码时,代码继续读取所有输入文件。然后我发现它在"for(i=0;i<nrTokeni;i++)
"循环中崩溃了。
我会看得更好一点,我真的很喜欢刚才的电视!:P
编辑:
好吧,我必须说这很有趣我将在未来的程序中使用该代码,该程序将在文件列表中搜索一些文本。我的电视在午夜结束,我发现你的颂歌很难阅读,所以根据你给出的需求描述,我决定重新实现它。我去掉了map和hash函数,发现它们对于我所理解的最终任务来说是不必要的。很可能(实际上是有希望的)代码有点不适合所提出的问题——它更多的是一个不同方法的例子,希望是一个带有更有意义命名的变量的代码的例子。
我发现变量名确实妨碍了我对它们的清晰理解。这似乎也比需要的要复杂一些。如果你有任何问题,我很乐意回答。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node_t
{
char *data;
node_t *next;
};
node_t *makeNewNode(char *newData)
{
node_t *tmp;
tmp = (node_t *)malloc(sizeof(node_t));
tmp->data = strdup(newData);
tmp->next = NULL;
return tmp;
}
void addNode(node_t **listHead, char *newData)
{
node_t *theNewNode = makeNewNode(newData);
if (*listHead == NULL)
*listHead = theNewNode;
else
{
node_t *curNode = *listHead;
while (curNode->next != NULL)
curNode = curNode->next;
curNode->next = theNewNode;
}
}
void printList(node_t *nodeList)
{
node_t *curNode = nodeList;
while (curNode != NULL)
{
printf("%sn", curNode->data);
curNode = curNode->next;
}
}
void readMainInputFile(char *filename, node_t **filesToProcessOut, node_t **searchTermsOut)
{
int numFiles, numSearchTerms;
int curFileNum, curSearchTerm;
const int maxLineLength = 1024;
char lineBuffer[maxLineLength+1], *endlStrippedFileName, *endlStrippedSearchTerms;
FILE *fileHandle = fopen(filename, "rt");
fscanf(fileHandle, "%dn", &numFiles);
for (curFileNum=0; curFileNum<numFiles; curFileNum++)
{
fgets(lineBuffer, maxLineLength, fileHandle);
endlStrippedFileName = strtok(lineBuffer, "rn");
addNode(filesToProcessOut, endlStrippedFileName);
}
fscanf(fileHandle, "%dn", &numSearchTerms);
for (curSearchTerm=0; curSearchTerm<numSearchTerms; curSearchTerm++)
{
fgets(lineBuffer, maxLineLength, fileHandle);
endlStrippedSearchTerms = strtok(lineBuffer, "rn");
addNode(searchTermsOut, endlStrippedSearchTerms);
}
fclose(fileHandle);
printf("Read %d files, %d search termsn", numFiles, numSearchTerms);
}
int fileLen(FILE *fp)
{
int result, curPos;
curPos = ftell(fp);
fseek(fp, 0, SEEK_END);
result = ftell(fp);
fseek(fp, curPos, SEEK_SET);
return result;
}
// searhes a file for any of the strings (seperated by | character) found in a single line from the inupt file.
// this is wasteful - we open load and search the file one time for each of the searchTerms.
// I.e - the InputData below would cause the file to be opened and read 4 times. Ideally, it should only be opened and read once
// we could fix this by passing a linked list of all of the lines of search terms - I'm too lazy. :-P
//11
//doctor & having
//I & hero | life
//innocently | that | know & will & it & I & yet
//shall & turn & out & to & be
bool doesFileContainSearchTerms(char *filename, char *searchTerms)
{
int fLen;
bool result;
char *buffer;
char *searchTermsCopy = strdup(searchTerms);
char *curToken, curSearchTerm[100];
bool spaceAtStart, spaceAtEnd;
// open file, get length, allocate space for length+1 bytes, zero that memory, read the file
FILE *fileHandle = fopen(filename, "rt");
fLen = fileLen(fileHandle);
buffer = (char*) calloc(1, fLen+1);
fread(buffer, fLen, 1, fileHandle);
fclose(fileHandle);
curToken = strtok(searchTermsCopy, "|");
while ((curToken != NULL) && (strlen(curToken)!=0))
{
memset(curSearchTerm, 0, 100);
// strip the leading/trailing spaces (and '|' char) from a search term
// e.g
// "I & hero |" --> "I & hero"
// " life" --> "life"
spaceAtStart = spaceAtEnd = false;
if ((curToken[0] == ' ') || (curToken[0] == '|'))
spaceAtStart = true;
if (curToken[strlen(curToken)-1] == ' ')
spaceAtEnd = true;
if ((spaceAtStart==false) && (spaceAtEnd==false))
strcpy(curSearchTerm, curToken);
else if ((spaceAtStart==false) && (spaceAtEnd==true))
strncpy(curSearchTerm, curToken, strlen(curToken)-1);
else if ((spaceAtStart==true) && (spaceAtEnd==false))
strncpy(curSearchTerm, curToken+1, strlen(curToken)-1);
else if ((spaceAtStart==true) && (spaceAtEnd==true))
strncpy(curSearchTerm, curToken+1, strlen(curToken)-2);
// printf("CurSearchTerm: ''%s''n", curSearchTerm);
// we're searching for _any_ of the text in the search term, e.g "I & hero | life"
// if we find one of them, then set result to true and stop looking.
result = false;
if (strstr(buffer, curSearchTerm) != NULL)
{
result = true;
break;
}
// didn't find one of the searchTerms yet, grab the next one
curToken = strtok(NULL, "|");
}
free(buffer);
free(searchTermsCopy);
return result;
}
int main()
{
node_t *inputFileList = NULL;
node_t *searchTermList = NULL;
readMainInputFile("input.in", &inputFileList, &searchTermList);
node_t *curFileNameNode = inputFileList;
while (curFileNameNode != NULL)
{
node_t *curSearchTermNode = searchTermList;
while (curSearchTermNode != NULL)
{
// printf("Searching %s for %sn", curFileNameNode->data, curSearchTermNode->data);
if (doesFileContainSearchTerms(curFileNameNode->data, curSearchTermNode->data))
printf("Search hit - file(%s), searchTerm(%s)n", curFileNameNode->data, curSearchTermNode->data);
curSearchTermNode = curSearchTermNode->next;
}
curFileNameNode = curFileNameNode->next;
}
}
输出:
Read 110 files, 11 search terms
Search hit - file(date.in), searchTerm(I & hero | life)
Search hit - file(date.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date2.in), searchTerm(I & hero | life)
Search hit - file(date2.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date3.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date4.in), searchTerm(I & hero | life)
Search hit - file(date4.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date6.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date6.in), searchTerm(looking | fire | called & another)
Search hit - file(date7.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date8.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date9.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date10.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date10.in), searchTerm(looking | fire | called & another)
Search hit - file(date11.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date12.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date13.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date14.in), searchTerm(looking | fire | called & another)
Search hit - file(date18.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date20.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date20.in), searchTerm(looking | fire | called & another)
Search hit - file(date23.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date25.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date25.in), searchTerm(looking | fire | called & another)
Search hit - file(date28.in), searchTerm(I & hero | life)
Search hit - file(date29.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date30.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date37.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date38.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date38.in), searchTerm(looking | fire | called & another)
Search hit - file(date44.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date45.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date47.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date50.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date52.in), searchTerm(I & hero | life)
Search hit - file(date52.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date53.in), searchTerm(looking | fire | called & another)
Search hit - file(date61.in), searchTerm(looking | fire | called & another)
Search hit - file(date68.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date75.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date76.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date76.in), searchTerm(looking | fire | called & another)
Search hit - file(date77.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date78.in), searchTerm(looking | fire | called & another)
Search hit - file(date81.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date84.in), searchTerm(looking | fire | called & another)
Search hit - file(date88.in), searchTerm(looking | fire | called & another)
Search hit - file(date89.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date91.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date92.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date100.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date102.in), searchTerm(innocently | that | know & will & it & I & yet)
Search hit - file(date110.in), searchTerm(innocently | that | know & will & it & I & yet)
Process returned 0 (0x0) execution time : 0.308 s
Press any key to continue.