コメントが長くなってしまいましたので、こちらに転載させていただきます。グラフィカル デバッガーが簡単に手に入るようになる前は、「トゥームストーン デバッグ」と呼ばれる手法を使用していました。基本的には、プログラムの実行が最後に到達した場所を特定するために、いくつかの printf ステートメントをコードに散りばめるだけです。
printf("(%d) %s\n", __LINE__, __FILE__);
ところで、これには非常に便利です。
より詳しく調べる必要があるコードを分離する簡単な方法として、あなたのコードでそれを行いました。最初のファイル「date.in」だけが読み取られていることがわかりました。その後、put_doc への 2 回目の呼び出しが返されないことがわかりました。
その後、あなたが Alocare_Mapare と Realocare_Mapare の更新されたコードを投稿したことに気付きました。:フェイスパーム:
新しい関数でコードを更新すると、コードはすべての入力ファイルの読み取りに進みました。for(i=0;i<nrTokeni;i++)
その後、「 」ループでクラッシュしていることがわかりました。
もう少し詳しく見ていきます。今のテレビがとても気に入っています。:P
編集:
さて、それは私が言わなければならないいくつかの楽しみでした。:) このコードは、ファイルのリストからテキストを検索する将来のプログラムに使用します。私のテレビは深夜に終了し、あなたの頌歌が読みにくいと感じたので、あなたが与えた要件の説明に基づいて、私はそれを再実装することにしました. 最終的なタスクであると理解していたものには不要であることがわかったため、マップ関数とハッシュ関数を廃止しました。コードが提示された質問にやや不適切である可能性が高い (実際には希望に満ちた) 可能性があります。これは、別のアプローチの例であり、できればより意味のある名前の変数を使用したコードの例です。
変数名が明確な理解の妨げになっていることがわかりました。また、必要以上に複雑に思えました。ご不明な点がございましたら、喜んでお答えいたします。
#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.