2014-09-20 60 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