2014-10-11 74 views
2

我是c編程的新手。我已經寫了使用fscanf()功能的程序:fscanf()函數錯誤

fscanf(fileptr, "%s\n", word); 

但是當我運行該程序就被卡在終端。我嘗試運行該程序GDB並顯示以下行:

109  for (int i = 0; i < 100; i++) 
(gdb) 
111   fscanf(fileptr, "%s\n", word); 
(gdb) 
__isoc99_fscanf (stream=0x603010, format=0x400cbd "%s\n") at isoc99_fscanf.c:26 
26 isoc99_fscanf.c: No such file or directory. 

,然後將這些行:

(gdb) 
_IO_vfscanf_internal ([email protected]=0x603010, format=0x400cbd "%s\n", [email protected]=0x7fffffffdd48, [email protected]=0x0) at vfscanf.c:225 
225 vfscanf.c: No such file or directory. 

是否存在與編譯器的任何問題,還是我失去了一些東西?我編譯:

gcc -ggdb3 -O0 -std=c99 -Wall -Werror hash.c -o hash 

下面是完整的代碼:

#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h> 
#include <ctype.h> 
#include <string.h> 

#define MAXLENGTH 10 

// structure for each node in hash 
typedef struct node 
{ 
    char* word; 
    struct node* next; 
} 
node; 

// global pointer for the root node 
node* root = NULL; 
node* table[26] = {}; 

// function declearations 
int hash_function(char data[MAXLENGTH]); 
void load_words(FILE* fileptr); 
bool check_word(char* word); 

// main function takes command line arguments 
int main(int argc, char** argv) 
{ 
    // validates user input at command line 
    if (argc != 2) 
    { 
     printf("Invalid Input!\n"); 
     printf("Usage : ./hash <word>\n"); 
     return 1001; 
    } 

    // loads the dictionary 
    FILE* file_ptr = fopen("dictionary.txt", "r"); 
    if (file_ptr == NULL) 
    { 
     printf("failed to load the file!\n"); 
     return 1002; 
    } 
    else 
    { 
     load_words(file_ptr); 
    } 

    // checks if the word is in dictionary 
    if (check_word(argv[1]) == true) 
    { 
     printf("Word spelled right!\n"); 
     return 0; 
    } 
    else 
    { 
     printf("Word spelled wrong!\n"); 
     return 0; 
    } 
} 

// hash function 
int hash_function(char data[MAXLENGTH]) 
{ 
    if (isalpha(data[0]) != 0) 
    { 
     int res = tolower(data[0]); 
     res = res - 'a'; 
     return res; 
    } 
    else 
    { 
     printf("not a word!\n"); 
     return -1; 
    } 
} 

// check_word function 
bool check_word(char* word) 
{ 
    int hash = hash_function(word); 
    if (hash != -1) 
    { 
     root = table[hash]; 

     while(root != NULL) 
     { 
      if (strcmp(word, root->word) == 0) 
      { 
       return true; 
      } 
     } 
    } 
    return false; 
} 

// load words into hash table 
void load_words(FILE* fileptr) 
{ 
    char word[10]; 

    for (int i = 0; i < 100; i++) 
    { 
     fscanf(fileptr, "%s\n", word); 

     int hash = hash_function(word); 

     if (hash != -1) 
     { 
      if (table[hash] == NULL) 
      { 
       table[hash] = malloc(sizeof(node)); 
       if (table[hash] == NULL) 
       { 
        printf("memory allocation failed!\n"); 
        return; 
       } 

       table[hash]->word = word; 
       table[hash]->next = NULL; 
      } 
      else 
      { 
       root = table[hash]; 

       while (root->next != NULL) 
        root = root->next; 

       node* newNode = malloc(sizeof(node)); 
       if (newNode == NULL) 
       { 
        printf("memory allocation failed!\n"); 
        return; 
       } 

       newNode->word = word; 
       newNode->next = NULL; 

       root->next = newNode; 
      }  
     } 
     else 
     { 
      printf("hash failed!\n"); 
     } 
    } 
} 

我使用Ubuntu 14.04和我已經安裝建立必要的。任何建議將非常感激。

在此先感謝。

+1

這些文件是'glibc'的*源代碼*的一部分,爲什麼你想要調試? – Amro 2014-10-11 05:55:26

+2

你應該考慮'fscanf'根據其規範正確運行(參見[fscanf(3)](http://man7.org/linux/man-pages/man3/fscanf.3.html)... )。所以你應該使用'gdb'的'next'命令。但是,您應該使用'fscanf'的結果。這些錯誤在您的代碼中(您不會顯示)。 – 2014-10-11 06:10:12

+0

爲什麼標記* compiler-construction *?這不相關!請**編輯您的問題**以改善它! – 2014-10-11 06:12:55

回答

3

check_word函數包含一個明顯的無限循環

root = table[hash]; 

    while(root != NULL) 
    { 
     if (strcmp(word, root->word) == 0) 
     { 
      return true; 
     } 
    } 

如果散列逆勢不是空的,第一個條目不匹配,它會永遠循環下去,因爲你永遠不沿碰撞推進root指針鏈。

您的代碼的另一個主要問題是在load_words函數中,您存儲了一個指向所有散列項中相同的本地word數組的指針。即所有node對象指向word指針的同一本地數組。這沒有意義,也沒有機會正常工作。當load_words函數退出時,本地word數組將被破壞,並且所有散列節點中的所有word指針都將無效。

每個散列節點必須擁有自己的word內存。你將如何實現這一點取決於你。要麼將node中的word作爲數組聲明,而不是指針,然後將strcpy本地word聲明爲節點的word。或者在散列節點中存儲每個單詞時使用strdup

+1

這是一個比答案更多的評論。 – 2014-10-11 06:57:33

+0

非常感謝。現在代碼正在工作,但我有一個邏輯問題。哈希表未按預期工作。但我會解決這個問題。再次感謝你。 – 2014-10-11 07:17:14

+0

非常感謝您的邏輯錯誤答案。 – 2014-10-11 07:26:40

1

你的另一個問題來自這行,因爲「字」是堆棧

table[hash]->word = word; 

您需要爲「字」如分配存儲在一個局部變量

table[hash]->word = strdup(word);