Ok, so here is the program now:
Code:
#include <stdbool.h>
#include <stdio.h>
#include "dictionary.h"
#include <string.h>
#include <stdlib.h>
#define MAXWORDS 26
#define DLENGTH 29
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char* word, char *words[LENGTH], int n)
{
{
int z;
int j,lo=0,hi=n-1,mid;
while(lo<=hi)
{
mid=(lo+hi)/2; //printf("lo: %d hi: %d mid: %d\n",lo,hi,mid);getchar();
j=strcmp(words[mid],text[z]);
if(j>0)
{
hi=mid-1;
}
else if(j<0)
{
lo=mid+1;
}
else
{
return true;
}
}
return false;
}
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char* dictionary)
{
int input(FILE *fp,char *words[DLENGTH],int getData);
char **words = NULL;
int count;
char buff[BUFSIZ]; //BUFSIZ or BUFSIZE is a macro for your system - usually 256 or 512 char's in size. A "natural" buffer length, for your system.
FILE *fp=fopen("DICTIONARY","r");
count=input(fp,words,0); //just counting this time
rewind(fp); //going back to the start of the file
//malloc the right number of words here
words=malloc(count * sizeof(char *));
for(int i=0;i<count;i++)
{
words[i]=malloc(DLENGTH * sizeof(char)); //#define LENGTH 29
}
input(fp,words,1); //now getting the words
//all the other stuff, here (mostly calling some functions)
printf("%s\n",buff);
return 0;
int input(FILE *fp, char *words[DLENGTH], int getData);
{
int i=0;
char buff[128];
while((fgets(buff, BUFSIZ, fp)) != NULL)
{
if(getData)
{
//remove the newline here
strcpy(words[i],buff);
}
++i;
}
if(getData==1)
return i;
else
return -1;
}
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
if (count > 1)
return count;
else
return 0;
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(FILE *fp)
{
//Is inptr the right thing to unload from memory???
if(fclose(fp)==0)
return true;
else
return false;
}
Here is my errors:
Code:
clang -ggdb -O0 -Qunused-arguments -std=c99 -Wall -Werror -c -o dictionary.o dictionary.c
dictionary.c:22:6: error: conflicting types for 'check'
bool check(const char* word, char *words[LENGTH], int n)
^
./dictionary.h:22:6: note: previous declaration is here
bool check(const char* word);
^
dictionary.c:29:27: error: use of undeclared identifier 'buff'
j=strcmp(words[mid],buff);
^
dictionary.c:77:17: error: use of undeclared identifier 'getData'
if(getData)
^
dictionary.c:84:12: error: use of undeclared identifier 'getData'
if(getData==1)
^
dictionary.c:98:5: error: use of undeclared identifier 'count'
if (count > 1)
^
dictionary.c:99:12: error: use of undeclared identifier 'count'
return count;
^
dictionary.c:106:6: error: conflicting types for 'unload'
bool unload(FILE *fp)
^
./dictionary.h:37:6: note: previous declaration is here
bool unload(void);
^
I hope I haven't messed to much up but what I have mainly been working on is "check". I was trying to get it to take the text input from speller.c (the other program that this one will be compiled with)
I seem to have caused LOTS of conflicting type errors, however....