0

Summary

I wrote some code which takes in a file with line separated words. This file contains every word in the dictionary, and is over 100000 lines long. I needed to do this so I could enter each word in a hash table and do a bunch of stuff, but that isn't important to my question. So the way I did this was reading the file using fopen, fread, etc, (the string which holds the files txt I will refer to as filetxt) and then splitting the filetxt string by \n characters using strtok to get each individual word. Problem is, strtok is splitting filetxt only once into two separate strings, as shown in a snippet of my output below. Why isn't it splitting every line?

Code

#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

unsigned int hash(const char *word) {
    // Find the place the first letter of {word} has in the alphabet (0 based)
    int pos = toupper(word[0]) % 32 - 1;
    return pos;
}

int main(void) {
    // Opening dictionary and writing the contents to a string
    FILE *file = fopen("dictionaries/large", "r");
    fseek(file, 0, SEEK_END);
    int len = ftell(file);
    rewind(file);
    char *filetxt = malloc(len + 1);
    fread(filetxt, len, 1, file);
   
    // Splitting every line
    char *token = strtok(filetxt, "\n");
    // declaring i for debugging purposes, seeing how many times the string is split
    int i = 0;
    while (token) {
        printf("%s", token);
        printf(" %i\n", i);
        token = strtok(NULL, "");
        i++;
    }
}

Output Snippet

a 0
aaa
aaas
aachen
aalborg
aalesund
aardvark
aardvark's
... (I left out the irrelevant 900000 lines of code)
zulu
zulu's
zulus
zurich
zurich's
zwilich
zwitterion
zwitterionic
zwitterions
zygote
zygote's
zygotes
zymurgy
 1

Dictionary File Snippet

https://i.stack.imgur.com/raX1V.jpg Please tell me if more of my dictionary is necessary.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
BrianZhang
  • 153
  • 1
  • 8

0 Answers0