I have an input file from which I need to extract words. Words can only contain letters and numbers, so everything else will be considered as a separator. I tried fscanf, fgets + sscanf and strtok but nothing works.
while(!feof(file)) { fscanf(file,"%s",string); printf("%s\n",string); }
Above one obviously does not work because it does not use delimiters, so I replaced the line like this:
fscanf(file,"%[Az]",string);
It reads the first word in order, but the file pointer continues to rewind so that it reads the first word again and again.
So, I used fgets to read the first line and used sscanf:
sscanf(line,"%[Az]%n,word,len); line+=len;
This does not work either because I try, I cannot move the pointer to the right place. I tried strtok but i cant find how to set delimiters
while(p != NULL) { printf("%s\n", p); p = strtok(NULL, " ");
This one obviously takes an empty character as a separator, but I have literally 100 separator units.
I missed something because extracting words from a file seemed like a simple concept at first, but does nothing I try really work?