Look at your code, you read all the data but you do not do anything with it:
int lineCount =0;
char c;
while ((c = fgetc(dictionary)) != EOF) {
if (c == '\n') {
lineCount++;
}
}
char word[50];
while (fgets(word, sizeof(word), dictionary)!= NULL) {
printf("%s", word);
}
}
So remove that first loop where you read it character by character, and just use the second loop to read and print the contents.
[edit]
OK, now we know what you are trying to do. So you first need to count the lines to know how much data to save. Once you have that then you need to allocate space to save the data. You do this by creating a dynamic array of
char*
types, and each one will then need to point to an array of characters. You then read each line into a new array and save its address in the array. Something like the following:
int linecount = 0;
char line[50]; while (fgets(line, sizeof(line), dictionary)!= NULL)
{
linecount++;
}
printf("%d lines read from file\n", linecount);
char** lines = (char**)malloc(linecount * sizeof(char*));
fseek(dictionary, 0, SEEK_SET); linecount = 0;
do
{
char* next = (char*)malloc(50); if (fgets(next, 50, dictionary) == NULL)
{
break; }
lines[linecount] = next; linecount++; } while (1);
int index;
for (index = 0; index < linecount; index++)
{
printf("%s", lines[index]);
}
There is no error checking in this code and there are a few things that need improving, but it should give you something to start with.
[/edit]