我是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和我已經安裝建立必要的。任何建議將非常感激。
在此先感謝。
這些文件是'glibc'的*源代碼*的一部分,爲什麼你想要調試? – Amro 2014-10-11 05:55:26
你應該考慮'fscanf'根據其規範正確運行(參見[fscanf(3)](http://man7.org/linux/man-pages/man3/fscanf.3.html)... )。所以你應該使用'gdb'的'next'命令。但是,您應該使用'fscanf'的結果。這些錯誤在您的代碼中(您不會顯示)。 – 2014-10-11 06:10:12
爲什麼標記* compiler-construction *?這不相關!請**編輯您的問題**以改善它! – 2014-10-11 06:12:55