Originally Posted by
Adak
So declare a FILE pointer variable, and use fopen("worklist.txt", "r"), and if the file pointer ("fp" is a good name), is valid after the fopen line of code, then you're free to start searching.
strcmp(string1, string2) will let you know when you have found the right word, by returning a zero. Think of it as "zero difference between the two strings being compared".
I tried what you said but I don't know how the txt file should look like or how to get the code to use the txt file.
To commonTater, I fixed that error where it had a limit on the amount of words the sentence could be. I have a char array with 'a unknown size' now.
Here is the code I have:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char *b = malloc(sizeof(char));
char * pch;
FILE * fp;
fp = fopen("worklist.txt", "r");
printf("Input a short sentence: ");
gets(b);
pch = strtok (b," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
getch();
return 0;
}
Adak, can I ask you to fix the code so it can read from the txt file and show me how the txt file should look?