0

好的,所以我有这个学校项目,我有一个很大的问题。我已经花了大约 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

拜托,我真的需要解决这个问题,我真的很失望。

4

1 回答 1

2

我的评论会有点长,所以我会在这里发布。在图形调试器如此容易获得之前,我们使用了一种称为“墓碑调试”的技术。基本上,您只需在代码中添加一些 printf 语句,以确定程序执行的最后一个位置。 printf("(%d) %s\n", __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("%s\n", 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, "%d\n", &numFiles);
    for (curFileNum=0; curFileNum<numFiles; curFileNum++)
    {
        fgets(lineBuffer, maxLineLength, fileHandle);
        endlStrippedFileName = strtok(lineBuffer, "\r\n");
        addNode(filesToProcessOut, endlStrippedFileName);
    }

    fscanf(fileHandle, "%d\n", &numSearchTerms);
    for (curSearchTerm=0; curSearchTerm<numSearchTerms; curSearchTerm++)
    {
        fgets(lineBuffer, maxLineLength, fileHandle);
        endlStrippedSearchTerms = strtok(lineBuffer, "\r\n");
        addNode(searchTermsOut, endlStrippedSearchTerms);
    }
    fclose(fileHandle);
    printf("Read %d files, %d search terms\n", 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 %s\n", 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.
于 2013-02-19T12:36:18.317 回答