Reading in word at a time from plain text file?
Posted on 2003-03-25
Hi the title basically sums up what I'm trying to do.
I am currently doing this:
int a =o;
inputPtr = fopen ("newfile.txt" , "r");
while((fgets (str , 40 , inputPtr)) != NULL)
token[a] = strtok(str," ");
printf("Current word to be checked is %s\n",token[a]);
From what I've read fgets will read in a line at a time from a text file (I've indicated it can be 40 chars long), and then token[a] will point to each word not seperated by a space (" "). So if my text file where to look like this:
Hello and welcome to the place where the answers are found and solutions are saught after
It would put:
token = Hello
token = and
token = welcome
Anyway you get the idea. This is what I am TRYING to get as my result, but am not.
It's currently jumping from one word in the file to another random one...
Any solutions would be MUCH appreciated.
By the way (in case it matters) I'm programming in C with linux using gcc as the compiler...?!