這是我第一篇文章,所以我提前感謝所有人提供任何幫助。消失的字符串
我遇到了消失的信息問題。該程序應該從文件中讀取書目標題和庫ID號,創建一個鏈接列表的哈希表,其中每個節點包含一個書結構,該書結構包含書的標題,另一個鏈接列表包含書ID所在的庫ID被發現。在這一點上,我只是試圖填充哈希表,但由於某種原因,我的標題值正在消失。文件格式如下:
2345,老人和海
567434,哈利·波特
1233,學習
等藝術...
我
typedef NodeStruct {void* data; NodePtr prev; NodePtr next;} NodeStruct;
typedef NodeStruct* NodePtr;
typedef ListStruct {NodePtr first; NodePtr last; NodePtr current; } ListStruct;
typedef ListStruct* ListHndl;
typedef BookStruct {char* title; ListHndl lib; } BookStruct;
typedef BookStruct* BookHndl;
FILE *fileptr;
int numlines;
int numslots;
int i;
int index;
NodePtr fillnode;
int main(int argc, char *argv[]) {
fileptr = fopen(argv[1], "r");
if (!fileptr) {
printf("Error: cannot open file\n");
return 1;
}
int *libID = malloc(sizeof(int));
printf("(main) libID: %p\n", libID);
fscanf(fileptr, "%d %d", &numlines, &numslots);
printf("(main) numlines: %d numslots: %d\n", numlines, numslots);
ListHndl *H = calloc(numslots, sizeof(ListHndl));
printf("(main) calloc iterated\n");
for (i = 0; i < numlines; i++) {
printf("*************LOOP #%d*************\n", i);
char *title = malloc(sizeof(char) * 50);
if (fscanf(fileptr, "%d%*c%*c%[^\n]", libID, title) == 0) {
printf("Error: incorrect # of keys\n");
return 1;
}
printf("(main) book: %s ID: %d\n", title, *libID);
index = hash(title, numslots);
printf("(main) index: %d\n", index);
printf("(main) hash[index]: %p\n", H[index]);
if (H[index] == NULL) {
H[index] = newList();
printf("(main_if_H_NULL) hash[index]: %p\n", H[index]);
BookHndl B = newBook(title, libID);
printf("(main_if_H_NULL) BookHndl B: %p\n", B);
insertAtFront(H[index],(void *) B);
printf("(main_if_H_NULL) H[index] title: %s ID: %d\n", ((BookHndl) H[index]->first->data)->title,
*((int *) ((BookHndl) H[index]->first->data)->lib->first->data));
}
printAll(H, numslots);
NodePtr S = searchTitle(H[index], title);
printf("(main) NodePtr S: %p\n", S);
if (S == NULL) {
BookHndl B = newBook(title, libID);
insertAtBack(H[index], (void *) B);
printf("(main_if_!S) inserted book\n");
} else {
printf("(main_if_S)\n");
ListHndl idL = ((BookHndl) S->data)->lib;
if (searchID(idL, libID) == 0) {
insertAtBack(idL, (void *) libID);
printf("(main_if_S) inserted libID\n");
}
}
free((char *) title);
free((NodePtr) S);
printAll(H, numslots);
printf("hash[%d]: %s in library # %d\n", index, title, *libID);
}
return 1;
}
實際上有四個fscanf參數%d用於libID,%* c跳過「,」,另一個%* c跳過「」,%[^/n]讀取標題直到讀取換行符。在確認libID和標題中的正確值之後不久,print語句。在printAll(H,numslots)中,所有值都存在,返回NodePtr S = searchTitle(H [index],title)後,標題消失。以下是我的新書和searchTitle功能
BookHndl newBook(char* str, int* ID) {
BookHndl B = malloc(sizeof(BookStruct));
/*B->title = malloc(sizeof(char) * 50);*/
B->title = strdup(str);
printf("(newBook) B->title = %s\n", B->title);
B->lib = newList();
printf("(newBook) B->lib = %p\n", B->lib);
insertAtFront(B->lib, (void *) ID);
printf("(newBook) B->lib->first = %d\n", *((int *)B->lib->first->data));
return B;
}
NodePtr searchTitle(ListHndl L, char* key) {
assert(L != NULL);
if (isEmpty(L)) {
printf("Book list empty\n");
return NULL;
} else {
L->current = L->first;
printf("(searchTitle) ListHndl L = %p, L->first = %p, L->first->data = %p\n", L, L->first, (BookHndl) L->first->data);
while (L->current != NULL) {
BookHndl B = malloc(sizeof(BookStruct));
B = (BookHndl) L->current->data;
printf("(searchTitle) bookhandle B = %p\n", B);
printf("(searchTitle) B->title = %s\n B->ID = %d\n", B->title, *((int *) B->lib->first->data));
if (strcmp(B->title, key) == 0) {
free(B);
return L->current;
}
free(B);
L->current = L->current->next;
}
}
return NULL;
}
減少英文單詞的數量...並向我們顯示代碼 - 這看起來像是內存泄漏或某些局部變量被破壞... – NirMH
'fscanf(fileptr,「%d%* c%* c%[^\ n]「,libID,name)'我計算了三個'%'符號和兩個參數。還有一個分號丟失。順便說一句,LibID是什麼?沒有定義它。最後:typedef被認爲是有害的... – wildplasser
另外,檢查scanf函數的返回值以檢測分析錯誤。 – hyde