1

I'm trying to write a C concordance program that reads words in from a file, strips them of non-alphanumeric characters, counts the number of times they occur, and prints them out, sorted and formatted, to a file containing the word and it's corresponding count in the text.

I'm running into this compiler error and I cannot figure out what the issue is, especially since it has no problem with the node *top in the previous method signature...

The error I'm getting is:

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

.h file:

#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

functions .c file

#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 file

#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;
}

Any help would be appreciated.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
rocksalad
  • 15
  • 3

1 Answers1

2

You're including your project header before <stdio.h>, so the FILE type isn't defined yet.

You need to either include <stdio.h> from your project header, or include your project header after <stdio.h>.

  • 1
    The 'either' is better; the 'or' is not a good idea. If people want to use the facilities of your header, they should be able to include your header without having to worry about which extra headers are necessary. See [Should I use `#include` in headers?](http://stackoverflow.com/questions/1804486/should-i-use-include-in-headers/1804719#1804719) for more discussion of this. That said, people from AT&T have stated that the 'or' is preferable, but I think the consensus is against them nowadays. – Jonathan Leffler Sep 20 '14 at 03:58
  • @JonathanLeffler That's absolutely correct. I meant to outline all the reasonable possibilities, though, not just the preferred one. –  Sep 20 '14 at 19:07