2013-02-15 28 views
1

我試圖實現的代碼是讀取.txt文件並將字符串轉換爲節點的方法。基本上,當我讀取.txt文件時,首先檢查非字母(單詞不能以數字開頭,單詞的任何索引中也不能有非字母數字)。一旦找到第一個字母,程序退出循環並進入另一個循環,直到看到一個空格。當我成功發表一個單詞時(當發現有空格時,單詞「結束」),我將該單詞輸入到鏈接列表中。總線錯誤:10從C中輸入文本從.txt文件到節點

當我運行這個,我得到一個總線錯誤:10.我認爲這將是由於單詞[b]數組,但是當我malloc它,我仍然得到相同的錯誤。

預先感謝您!

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

#define TRUE 1 
#define FALSE 0 


struct Node{ 
    char value[100]; 
    int numOccur; 
    int numVariance; 
    struct Node *next; 
}; 

void testprint(struct Node * head){ 
    int data; 
    data = head->value; 
    strcpy(data,head->value); 
    while(head != NULL){ 

     printf("%s\n", data); 
     head = head->next; 
    } 

} 

int main() 
{ 
    struct Node *curr; 
    struct Node *n; 
    struct Node *head =0; 
    struct Node *tail =0; 
    struct Node *next; 
    char word[100]; 
    int a; 
    int x; 



    FILE *file1; 
    file1 = fopen("test1.txt", "r");   //opens text file 

    if(file1 == NULL){ 
     fprintf(stderr,"Error: Could not open file");  //if file1 has error, returns error message 
    exit(1); 
    } 
    a = fgetc(file1); 
    int b = 0; 
    while(a != EOF){ 
     while(!isalpha(a)){ 
      a = fgetc(file1); 
      continue; 
     } 

     n = (struct Node *) malloc(sizeof(struct Node)); 
     while(isalnum(a)){ 
      while(a != ' ' && a != EOF){ 
       word[b] = a; 
       a = fgetc(file1); 
       b++; 
      } 
      word[b] = '\0'; 
     } 
     n->next = 0; 
     if(head == 0){ 
      head = n; 
      tail = n; 
     } 
     else{ 
      tail->next = n; 
      tail = n; 
     } 
    } 
    testprint(head); 
    fclose(file1); 
} 
+1

你的代碼甚至沒有編譯。有很多錯誤。當期望Node *時,您可以使用FILE *調用testprint()。此外,當您執行word [b] =(char *)malloc(sizeof(char)* 30)時,數據類型不匹配。您正在分配一個動態字符數組(lhs)並將其分配給char(rhs)。 – Barney 2013-02-15 04:25:26

+0

在這段代碼中錯誤的東西中,'/ 0'不是終止的空字符; '\ 0'是。事實上,爲了避免*再次出現問題,請不要使用* *。改用'0'。另外,當你到達你的第一個單詞時,你的意圖是分配一個新的30字符緩衝區*爲永遠的字符*?似乎有點矯枉過正,不是嗎?特別是考慮到你將配置保存在一個無效的內存區域(類型錯誤)並且在過程中像篩子一樣泄漏。把這個[codereview.stackexchange.com](http://codereview.stackexchange.com)並修復* real *問題。 – WhozCraig 2013-02-15 04:28:31

回答

3

您應該注意編譯器的警告。 (最好總是用-Wall -Wextra進行編譯以獲得更多警告。)

正如Barney Hsiao指出的,testprint()是用FILE指針調用的,而不是節點指針。


無關,但如果你需要真正常量,有一個標準的頭文件,它會給你這些(小寫,即truefalse)。

#include <stdbool.h> 

我編譯此代碼時,遇到下列編譯器警告:

$ CFLAGS="-Wall -Wextra" make bus10 
cc -Wall -Wextra bus10.c -o bus10 
bus10.c: In function ‘testprint’: 
bus10.c:20:14: warning: assignment from incompatible pointer type 
bus10.c: In function ‘main’: 
bus10.c:48:8: warning: array subscript has type ‘char’ 
bus10.c:54:8: warning: array subscript has type ‘char’ 
bus10.c:68:23: warning: assignment from incompatible pointer type 
bus10.c:34:8: warning: unused variable ‘x’ 
bus10.c:31:17: warning: unused variable ‘next’ 
bus10.c:27:17: warning: unused variable ‘curr’ 
bus10.c:74:1: warning: control reaches end of non-void function 

現在,其中的一些人比其他人更嚴重。 「數組下標具有類型'char'」對我來說聽起來不是一個問題(說實話,我不知道它甚至意味着什麼)。 「未使用的變量」意味着:你已經創建(聲明)了一個變量,然後你永遠不會使用它。 「控制到達非空函數結束」意味着你有一個被聲明爲返回一個值的函數,但是代碼在沒有返回語句的情況下脫離底部。

但是「從不兼容的指針類型分配」是不好的。單詞不兼容是不好的。 20行顯示:68

 head = head->next; 

行寫着:

 tail->next = n; 

這是因爲

struct Node{ 
    ... 
    struct node *next; 
}; 

看到了嗎? *next不是struct Node而是struct node。大寫/小寫計數。


好的。這是一個很大的問題。編譯器也沒有說什麼。那麼,公平就是這樣。但我們不知道這意味着什麼。第33行:

char a; 

你說什麼錯?這是一個擁有角色的類型,對吧?問題在這裏:

a = fgetc(file1); 

你看到它嗎?這裏怎麼樣?

while(a != EOF){ 

EOF是一個哨兵值過大,可安裝在char。這應該是這樣的。爲了使fgetc函數告訴你文件結束條件已經發生,而不是文件中的另一個字節,它必須返回一個超出char範圍的值(0..255無符號,-128..127有符號)。 while條件將永遠不會失敗,因爲沒有char值可以比較等於EOF。

所以聲明aint而不是char

+0

冰山一角。 – WhozCraig 2013-02-15 04:33:59

+0

同意。但是,首先*首先*。 – 2013-02-15 04:35:14

+1

那麼在這種情況下,'struct node * next;',然後是'int a'而不是'char a',最多在while循環中,while(a!=''){'不測試EOF所以它超過了字符緩衝區與EOF(這就是總線錯誤,順便說一句)。等。 – WhozCraig 2013-02-15 04:36:18