2016-11-01 70 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並返回給調用者。