2014-10-11 52 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);