Here's a sample code that I coded up (quite messy since I rushed it a bit )
Then again, it's better than nothing so! Note this uses fscanf() to control the loop not feof(), if you "must" use feof() then you should do something about the irregularities of using feof(). Such as being "1 loop" late at noticing the EOF indicator, which means you should minus 1 from count and discard the last reading. Even if your professor mentioned to use feof() I would recommend sending him an e-mail about it because like Quzah said it's wrong to do.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int sequential_search(char* word_table[], char* key, int word_count);
void initialisation(char* objects[]);
int main(void)
{
FILE* fp;
int fileselect;
char filename[BUFSIZ];
char* words[10000];
int word_count = 0;
int i;
int j;
while(1)
{
printf("\nSelect filename: \n");
printf("1 - words_death\n");
printf("2 - words_live\n");
printf("3 - Quit\n");
scanf("%d",&fileselect);
printf("\n");
switch(fileselect)
{
case 1:
strcpy(filename,"words_death.txt");
break;
case 2:
strcpy(filename,"words_live.txt");
break;
case 3:
default:
return 0;
}
fp = fopen(filename,"r");
if (fp == NULL)
{
fprintf(stderr,"Could not open!");
return 1;
}
initialisation(words);
/* Read all words */
while (fscanf(fp,"%s",words[word_count]) != EOF)
{
word_count++;
}
i = word_count;
printf("\n\nThe first ten words: \n");
/* Print first ten words */
for (j = 0; j < 10 && j < i; j++)
{
printf("%d:\t %s\t\t\n",j+1,words[j],
sequential_search(words,words[j],word_count));
}
printf("\n\nThe last ten words: \n");
if (i > 10)
j = i - 10;
else
j = 0;
/* Print last ten words */
for ( ; j < i ; j++)
{
printf("%d:\t %s\t\t%d\n",j+1, words[j],
sequential_search(words,words[j],word_count));
}
/* Echo the amount of words */
printf("\n\nOverall amount of words in file %s: %d\n",
filename,word_count);
if (fclose(fp) == EOF)
{
fprintf(stderr,"Could not close!");
return 1;
}
return 0;
}
}
int sequential_search(char* word_table[], char* key, int word_count)
{
int i;
int instances = 0;
for (i = 0; i < word_count; i++)
{
if (strcmp(key,word_table[i]) == 0)
instances++;
}
return instances;
}
void initialisation(char* objects[])
{
int i;
for (i = 0 ; i < 10000; i++){
objects[i] = malloc(sizeof(char) * BUFSIZ);
}
}
I've used different filenames to what you did but it shouldn't really matter. The sequential search algorithm is just used to count how many instances the word appears in the file after all the words are read in by the fscanf() function.
Heres a sample output:
Code:
bash-3.00# ./file1
Select filename:
1 - words_death
2 - words_live
3 - Quit
1
The first ten words:
1: hmmm 1
2: this 1
3: is 1
4: a 2
5: word 1
6: that 2
7: I 7
8: should 1
9: never 1
10: forget 1
The last ten words:
55: was 1
56: only 1
57: a 2
58: dream.. 1
59: dammit! 1
60: I 7
61: dreamt 1
62: for 1
63: 45 1
64: years! 1
Overall amount of words in file words_death.txt: 64
There are some small formatting issues that I would leave up to you to fix. It's primarily to give you an idea on how to approach the problem. I recommend you code small sections of the problem to see how it works before putting that section into your main code, it helps in both debugging and understanding the problem.
After you have implemented up to this stage with your files and how you want it formatted etc. you will need to make a sorting function or simply use the qsort() function from C as anonytmouse mentioned. Simply send in the strcmp() function as a function pointer to the qsort() function if you are using it.