Hi all. I am stumped by the warnings and errors for the "countwords" function.
Code:
// wordcount.c: count the number of occurrences of every word in a text file
// 4 functions: openfile, countwords, sort, display
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define FILENAMELENGTH 30
#define SUCCESS 1
#define FAILURE 0
FILE *file;
int openfile(char *filename);
void countwords(void);
void sort(char *filename);
void display(char *filename);
int main(void)
{
int result;
char filename[FILENAMELENGTH];
do
{
printf("Enter file to open: ");
fgets(filename, FILENAMELENGTH, stdin);
filename[strlen(filename) - 1] = '\0';
} while((result = openfile(filename)) == FAILURE);
countwords();
sort(filename);
display(filename);
return 0;
}
int openfile(char *filename)
{
if((file = fopen(filename, "r")) == NULL)
return FAILURE;
else
{
printf("%s opened successfully", filename);
return SUCCESS;
}
}
void countwords(void)
{
#define WORDLENGTH 30
char buffer[WORDLENGTH];
int c, count;
struct word
{
char *word;
int wordcount = 0;
} array[500];
while((c = fgetc(file)) != EOF)
{
if((c != '.') && (c != ',') && (c != '!') && (c != "") && (c != ':')) //if c is not a space or punctuation, add c to buffer[]
{
buffer[count] = c;
count++;
}
else
{
buffer[count++] = '\n'; // if c is a space or punctuation, add terminator to create string
array[count].word = malloc((count) * sizeof(char)); // allocate memory for string
*array[count].word = buffer[count]; // assign string to pointer in structure
array[count].wordcount++; // increment counter for word
}
}
}
void sort(char *filename)
{
}
void display(char *filename)
{
}
Code:
|In function ‘countwords’:|
59|error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token|
64|warning: comparison between pointer and integer [enabled by default]|
64|warning: comparison with string literal results in unspecified behaviour [-Waddress]|
75|error: ‘struct word’ has no member named ‘wordcount’|
||=== Build finished: 2 errors, 2 warnings ===|
1. What does it mean by expected ; etc in line 59? I can't see any missing punctuation.
2. For line 64, fgetc returns an integer, and I am comparing it with characters(which are integers), so why does it mention pointer?
3. Also, for line 65, "string literal" means "", right? When I use ' ', I get an error message "empty character constant". I just want to compare c to empty white space.
4. There is a "wordcount" member in the struct, so why the error message?
Thanks in advance.