2014-09-20 29 views
1

我正在嘗試編寫一個C語言和解程序,用於從文件中讀取單詞,去除它們的非字母數字字符,統計它們出現的次數並打印出來,排序和格式化爲包含單詞的文件,並且它在文本中是相應的計數。在.h文件中的方法簽名中的語法錯誤

我運行到這個編譯器錯誤,我想不通的問題是什麼,特別是因爲它具有與節點沒有問題*頂在前面的方法簽名......

我的錯誤得到的是:

proj1f.h:12: error: syntax error before "FILE"

.h文件:

#ifndef PROJ1F_H 
#define PROJ1F_H 

typedef struct node { 
    char *data; 
    struct node *left; 
    struct node *right; 
} node; 

void insert(char *x, node *top, int count); 

void print(node *top, FILE *file, int *count, int index); 

#endif 

功能.c文件

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

void insert(char *x, node *top, int count){ 
    if(top == NULL){ //place to insert 
    node *p = malloc(sizeof(node)); 
     p -> data = x; 
     p -> left = p-> right = NULL; 
     top = p; 
    count++; 
    } 
    else if(x == top -> data) 
     count++; 
    else if(x < top -> data) 
     insert(x, top -> left, count); 
    else //x > top -> data; 
     insert(x, top -> right, count); 
} 

void print(node *top, FILE *file, int *count, int index){ 
    if(top == NULL) 
     fprintf(file, "%s", "no input read in from file"); 
    else{ 
     print(top -> left, file, count, index++); 
     fprintf(file, "%-17s %d\n", top -> data, count[index]); 
     print(top -> right, file, count, index++); 
    } 
} 

Main .c文件

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


int main(int argc, char *argv[]) { 
int count[300]; 
int index = 0; 
int wordInFile = 0; 
node *root = NULL; 
FILE * readFile = fopen(argv[1], "r"); 

while(feof(readFile)) { 
    char word[30]; 
    char fword[30]; 
    fscanf(readFile, "%s", word); 

    //format word 
    int findex = 0; 
    int i; 
    for(i = 0; i < strlen(word); i++) { 
    if(isalnum(word[i])) { 
     fword[findex] = word[i]; 
     findex++; 
    } else if(word[i] == NULL) { 
     fword[findex] = word[i]; 
     break; 
    } 
    } 

    //insert into tree 
    insert(fword, root, count[wordInFile]); 
    wordInFile++; 
} 

fclose(readFile); 
FILE *writeFile = fopen(argv[2], "w+"); 
print(root, writeFile, count, index); 
fclose(writeFile); 

return 0; 
} 

任何幫助,將不勝感激。

回答

2

您在<stdio.h>之前包含項目標題,因此尚未定義FILE類型。

您需要在項目標題中包含<stdio.h>,或在<stdio.h>之後包含項目標題。

+1

'或'更好; '或'不是一個好主意。如果人們想要使用標題的設施,他們應該能夠包含標題,而不必擔心需要額外的標題。請參閱[我應該在頭文件中使用'#include'](http://stackoverflow.com/questions/1804486/should-i-use-include-in-headers/1804719#1804719)以獲取更多關於此的討論。也就是說,來自AT&T的人表示,'或'是可取的,但我認爲目前的共識是對他們的。 – 2014-09-20 03:58:54

+0

@JonathanLeffler這絕對正確。我打算概括所有合理的可能性,但不僅僅是首選。 – duskwuff 2014-09-20 19:07:22