2016-11-01 36 views
0

在下面的代码中,我试图加载一个字符的文本文件的字符 然后我试图保存每个整个单词散列表(字符串数组) 但似乎strcpy保存整个单词不是一个单一的char,我不知道为什么。我是否滥用strcpystrcat在C中使用strcpy,strcat的冲突?

# include <stdio.h> 
# include <stdlib.h> 
# include <string.h> 
# include <ctype.h> 
# include <stdbool.h> 
bool load(const char* dictionary); 

#define LENGTH 45 


int main (int argc, char* argv[]) 
{ 
    char* dictionary = argv[1]; 
    load(dictionary); 
    return 0; 
} 

bool load(const char* dictionary) 
{ 
    int index = 0, words = 0, kk = 0; 
    int lastl = 0, midl = 0; 
    char word[LENGTH + 1]; 
    char *wholeword[1001]; 

    FILE* dic = fopen(dictionary, "r"); 
    if (dic == NULL) 
    { 
    printf("Could not open %s.\n", dictionary); 
    return false; 
    } 

    for (int c = fgetc(dic); c != EOF; c = fgetc(dic)) 
    { 
    // allow only alphabetical characters and apostrophes 
    if (isalpha(c) || (c == '\'' && index > 0)) 
    { 
     // append character to word 
     word[index] = c; 
     index++; 

     // ignore alphabetical strings too long to be words 
     if (index > LENGTH) 
     { 
     // consume remainder of alphabetical string 
     while ((c = fgetc(dic)) != EOF && isalpha(c)); 
     // prepare for new word 
     index = 0; 
     } 
    } 

    // ignore words with numbers (like MS Word can) 
    else if (isdigit(c)) 
    { 
     // consume remainder of alphanumeric string 
     while ((c = fgetc(dic)) != EOF && isalnum(c)); 

     // prepare for new word 
     index = 0; 
    } 

    // we must have found a whole word 
    else if (index > 0) 
    { 
     // terminate current word 
     word[index] = '\0'; 
     lastl = index - 1; 
     midl = (index - 1) % 3; 
     words++; 
     index = 0; 

     int hashi = (word[0] + word[lastl]) * (word[midl] + 17) % 1000; 

     wholeword[hashi] = (char*) malloc(sizeof(char) * (lastl + 2)); 

     strcpy(wholeword[hashi], &word[0]); // *** 

     for (kk = 1; kk <= lastl + 1; kk++) 
     { 
     strcat(wholeword[words], &word[kk]); 
     } 
    } 
    } 
    fclose(dic); 
    return true; 
} 
+0

很难理解你的问题是什么。顾名思义,'strcpy'函数复制一个字符串。什么是'wword'? –

+0

你是否尝试用调试器逐步完成代码? – pm100

+0

@DavidSchwartz wword是我在这里编写的错字(我编辑它)是整个字符串(字符串数组),谢谢 –

回答

2

STRCPY不复制单个字符,它会将所有的字符,直到下一个空('\0')字节。在代码中尝试复制单个字符:代替

wholeword[hashi] = &word[0]; 

strcpy(wholeword[hashi], &word[0]); 
+0

请不要在数组下标之前放置空格 - 它们绑定非常紧密,不应该像这样间隔。例如,在'&word [0]'中,优先级是'&(word [0]'),而不是'(&word)[0]'。 –

0

是的,你是滥用strcpystrcat:这些功能复制整个源字符串到目的阵列(末尾现有的字符串为strcat)。

下列行:

wholeword[hashi] = (char*) malloc(sizeof(char) * (lastl + 2)); 

    strcpy(wholeword[hashi], &word[0]); // *** 

    for (kk = 1; kk <= lastl + 1; kk++) 
    { 
    strcat(wholeword[words], &word[kk]); 
    } 
} 

可以与单个呼叫被替换以

wholeword[hashi] = strdup(word); 

strdup()分配存储器,复制参数字符串到它,并返回指针。它适用于所有POSIX系统,如果没有它,使用这两条线:

wholeword[hashi] = malloc(lastl + 2); 
    strcpy(wholeword[hashi], word); 

注:

  • 你假设你的散列是完美的,没有冲突。按照目前的编码,碰撞导致先前的单词从字典中删除,并且其相应的内存将丢失。
  • 字典char *wholeword[1001];load函数中的局部变量。它是未初始化的,因此无法知道条目是否是指向某个单词的有效指针。它应该被分配,初始化为NULL并返回给调用者。