I am trying to read thousands of words and print only the first 15 but the only word that it prints is the very last one in the array where I stored the words.
g++ --version g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
From the following answer I derived the code below to read a file line by line C read file line by line
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
unsigned char *words[25143];
int readDictionary(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
int count = 0;
fp = fopen("dictionary.txt", "r");
if (fp == NULL)
{
printf("failed to open file");
exit(EXIT_FAILURE);
}
while ((read = getline(&line, &len, fp)) != -1) {
printf("%s", line);
words[count] =(unsigned char*) line;
count++;
}
fclose(fp);
if (line)
free(line);
}
int main (void)
{
readDictionary();
printf("we just read the dictionary\n");
for (int k= 0; k <15; k++)
{
printf("%d %s",k,(unsigned char*)words[k]);
}
}