I'm trying to make a dictionary data structure program in C that will basically put a text file (with any amount of lines) into a struct.
The dictionary recognises the first word of a line as a word and the rest of the line as a description for that word - obviously the whole line being an entry into the dictionary.
The text file ends with one line with a full stop and that is when the dictionary stops reading the file.
This is the file so far.
[cpp]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_SIZE 40
#define MAX_DESC_SIZE 200
void d_initialise() {
struct dictionary {
char word[MAX_WORD_SIZE];
char desc[MAX_DESC_SIZE];
struct dictionary *next; // next entry
};
}
/** returns true if the file is read */
int d_read_from_file(const char * filename) {
d_initialise();
struct dictionary *dict;
FILE *file = fopen(filename, "r");
char line[MAX_WORD_SIZE + MAX_DESC_SIZE + 1];
char *buffer;
int i=0;
if (file == NULL) {
printf("Error reading file.");
return 0;
} else {
while ((fgets(line, sizeof line, file) != NULL) != ".") {
if ((fscanf(file, "%s %[^\n]", dict->word, dict->desc)) != '\n') {
buffer[i] = dict->next;
i++;
}
}
fclose(file);
}
return 1;
}
/** search dictionary for a word and meaning */
int d_lookup(const char * word, char * desc) {
}
[/cpp]
And this is an example text file:
[code]
computer Electronic device for processing data according to instructions
playground Area of outdoor play or recreation
plateau Land area with high, level surface
aardvark Unfriendly, nocturnal mammal native to Africa
.
[/code]
Compilation errors at the moment:
[code]
dictionary.c: In function ‘d_read_from_file’:
dictionary.c:32:57: warning: comparison between pointer and integer [enabled by default]
while ((fgets(line, sizeof line, file) != NULL) != ".") {
^
dictionary.c:33:48: error: dereferencing pointer to incomplete type
if ((fscanf(file, "%s %[^\n]", dict->word, dict->desc)) != '\n') {
^
dictionary.c:33:60: error: dereferencing pointer to incomplete type
if ((fscanf(file, "%s %[^\n]", dict->word, dict->desc)) != '\n') {
^
dictionary.c:34:33: error: dereferencing pointer to incomplete type
buffer[i] = dict->next;
^
dictionary.c: At top level:
dictionary.c:54:7: error: expected identifier or ‘(’ before ‘else’
} else {
^
dictionary.c:73:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
[/code]
Thanks in advance!
Move your dictionary struct definition out of d_initialise() into the global scope, otherwise d_read_from_file() does not see it.
I changed d_read_from_file to this:
[cpp]
int d_read_from_file(const char * filename) {
struct dictionary {
char word[MAX_WORD_SIZE];
char desc[MAX_DESC_SIZE];
struct dictionary *next; // next entry
};
struct dictionary *dict;
FILE *file = fopen(filename, "r");
char line[MAX_WORD_SIZE + MAX_DESC_SIZE + 1];
char *buffer;
int i=0;
if (file == NULL) {
printf("Error reading file.");
return 0;
} else {
while ((fgets(line, sizeof line, file) != ".") {
if ((fscanf(file, "%s %[^\n]", dict->word, dict->desc)) != '\n') {
buffer[i] = dict->next;
i++;
}
}
fclose(file);
}
return 1;
}
[/cpp]
And these errors occur when compiling:
[code]
dictionary.c: In function ‘d_read_from_file’:
dictionary.c:36:56: error: expected ‘)’ before ‘{’ token
while ((fgets(line, sizeof line, file) != ".") {
^
dictionary.c:43:5: error: expected expression before ‘}’ token
}
^
dictionary.c: At top level:
dictionary.c:58:7: error: expected identifier or ‘(’ before ‘else’
} else {
^
dictionary.c:77:1: error: expected identifier or ‘(’ before ‘}’ token
}
^
[/code]
Well, that error message tells you exactly what's missing.
[editline]20th March 2014[/editline]
Besides, this is not how string comparison works in C:
[cpp]
while ((fgets(line, sizeof line, file) != ".") {
[/cpp]
You are comparing two pointers to char arrays, not the string contents. Additionally, this will never evaluate to true, as one of the pointers points to line - which is stored in the stack, being locally declared - and the other one points to a string somewhere in a read-only memory section. They'll never ever be equal. Use [url=http://www.cplusplus.com/reference/cstring/strcmp/]strcmp()[/url] to compare two strings.
[editline]20th March 2014[/editline]
And last but not least, don't forget to move the struct definition into [i]global scope[/i]. That's where it belongs, not in the body of a function. It needs to go outside.
Sorry, you need to Log In to post a reply to this thread.