> typedef char string7[8];
But "History\n\0" (as returned by fgets()) is 9 characters, not 8
So my first set of results look like this
Code:
$ cat new.txt && ./a.exe
History
Noun
7
2
a telling of events
a written report of past events
Disband
Verb
7
1
to break up and stop being a group
0
word=History
type=
Num string=Noun, val=0
Num string=7, val=7
1
word=to brea
type=k up and stop b
Num string=eing a group, val=2089872920
Num string=, val=2089878896
It's already gone wrong by the time the first type is read, so after that everything is out of sync.
By allowing an extra space in the strings, I get
Code:
0
word=History
type=Noun
Num string=7, val=7
Num string=2, val=2
1
word=Disband
type=Verb
Num string=7, val=7
Num string=1, val=1
Here's the full code - note that your newline clearing code always used the same array (more copy/paste errors).
Code:
#include <stdio.h>
#include <string.h>
// +1 for the \n fgets() returns, which we eliminate
// +1 for the \0 at the end of every string
typedef char string7[8+1+1];
typedef char string15[16+1+1];
typedef char string50[51+1+1];
typedef struct //structure of the word
{
string7 strWord;
string15 strType;
int nLetters;
int nDefinitions;
string50 strDefinitions[5];
} structWord;
char *fgetsNoNewline(char *buff, size_t bufsiz, FILE * fp)
{
char *result;
result = fgets(buff, bufsiz, fp);
if (result != NULL) {
char *p = strchr(buff, '\n');
if (p != NULL)
*p = '\0';
}
return result;
}
void loadDictionary(structWord aWords[], int nWordNumber, FILE * pFile)
{
int i, j;
char temp[BUFSIZ];
for (i = 0; i < nWordNumber; i++) {
printf("%d\n", i);
fgetsNoNewline(aWords[i].strWord, sizeof(aWords[i].strWord), pFile);
printf("word=%s\n", aWords[i].strWord);
fgetsNoNewline(aWords[i].strType, sizeof(aWords[i].strType), pFile);
printf("type=%s\n", aWords[i].strType);
fgetsNoNewline(temp, sizeof(temp), pFile);
sscanf(temp, "%d", &aWords[i].nLetters);
printf("Num string=%s, val=%d\n", temp, aWords[i].nLetters);
fgetsNoNewline(temp, sizeof(temp), pFile);
sscanf(temp, "%d", &aWords[i].nDefinitions);
printf("Num string=%s, val=%d\n", temp, aWords[i].nDefinitions);
for (j = 0; j < aWords[i].nDefinitions; j++) {
fgetsNoNewline(aWords[i].strDefinitions[j], sizeof(aWords[i].strDefinitions[j]), pFile);
}
}
}
int main()
{
structWord arr[2];
FILE *fp = fopen("new.txt", "r");
if (fp != NULL) {
loadDictionary(arr, 2, fp);
fclose(fp);
// print a couple of samples
printf("%s\n", arr[0].strWord);
printf("%s\n", arr[1].strDefinitions[0]);
}
return 0;
}
Actually, because you allocate such small space, your code is very vunerable to minor formatting mistakes in the input file.
So where you have
fgetsNoNewline(aWords[i].strWord, sizeof(aWords[i].strWord), pFile);
It should really be
fgetsNoNewline(temp,sizeof temp, pFile);
temp[ sizeof(aWords[i].strWord) - 1] = '\0'; // make sure the string is always short enough
strcpy( aWords[i].strWord, temp ); // and copy it.
This truncate and copy should be done as a separate function as well.
Also, sscanf returns a result as well, which you should check before trying to use the converted answer.