This is a minimised repost of a question i asked earlier. I am a beginner in C programming. I am attempting to create a Countdown program but in which the user selects eight consonants and/or vowels and has to devise the longest word from these letters. The computer will then read a dictionary file and find the longest possible words. This function is a part of the program in which I compare the countdown letters with a dictionary file.
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i, j, n;
long int n1 = 0, n2 = 0, n3 = 0, n4 = 0, n5 = 0, n6 = 0, n7 = 0, n8 = 0;
char line[9];
char exampleLetters[] = "feacnehp";
char *fileName = "D:\\webster.txt"; //Dictionary file
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Error opening file!\n");
}
else
{
while (!feof(fp)) {
fgets(line, 9, fp);
n = 0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
if (line[i] == exampleLetters[j]) n++;
}
}
if (n == 1) n1++; //These values are incremented everytime a word of that amount of letters is found i.e. n1++ when a one letter word is found
if (n == 2) n2++;
if (n == 3) n3++;
if (n == 4) n4++;
if (n == 5) n5++;
if (n == 6) n6++;
if (n == 7) n7++;
if (n == 8) n8++;
}
printf("%li %li %li %li %li %li %li %li\n", n1, n2, n3, n4, n5, n6, n7, n8); //This is irrelevant but just to display the amount of each number of words
}
fclose(fp);
return 1;
}
My problem is in the readFile function. I'm not sure how to compare the countdown letters with the dictionary file. I am able to count the amount of words that match with the letters. Should I read the words in and use malloc to continually allocate memory or is there a better alternative?