Set up a variable for a count, set up a while..do loop to read each byte until the EOF. for each byte it reads compare it with the bytes stored in a char array. if the first byte in the array matches the byte in the file, then read the next char and compare with the next byte in the array. If all bytes match increment counter. If there is even 1 byte that does not match in the array, break the loop and start over at the next byte.
When you're done, the count variable should hold the number of word accurences. If this confuses you, because it's kind of sloppy, written horribly and incredibly fast, I will re-write this with example code. Just let me know if this helps at all.
int main (void )
{
char *word;//this is the search string
char *string;//this is the string from the file
FILE *f;
int number=0;
printf("Please enter the word to search):
scanf("%s",word);
f=fopen("filename.txt","r"
if (f==NULL)
{
perror("Can't open file"
exit(1);
}
while(!feof(f))
{
fscanf(f,"%s",string);
if(!strcmp(string,word))//if match found
number++:
}
printf("We found the word %s in the file %d times",word,number);
return 0;
}
Three errors here. No memory for searchStr is allocated, so gets() will write into unitialized memory, No memory is allocated for word which is used later in the example and any use of gets() is a bug because the programmer can't place a limit on the number of characters entered by the user. Instead, something like this:
char searchStr[50]; /* 50 or whatever */
if (fgets(searchStr,sizeof searchStr,stdin)!=NULL) {
/* ... */
Or if you want to use pointers:
char *searchString=malloc(51); /* allocate for 50 characters */
if (searchStr!=NULL) {
if (fgets(searchStr,51,stdin)!=NULL) {
/* ... */
Of course, 50 should be replaced with a symbolic constant or be stored as a variable.
Next example:
>char *word;//this is the search string
>char *string;//this is the string from the file
>FILE *f;
>int number=0;
>printf("Please enter the word to search):
>scanf("%s",word);
Same thing here, scanf() writes into unitialized memory because word doesn't point anywhere valid. Also, there is no limit placed on the number of characters you can enter, as in gets().
Either:
char word[50];
scanf("%50s",word);
Or just use fgets().
>while(!feof(f))
The problem is that feof() tests the stream *before* it's been evaluated, so the fscanf() call will hit EOF before the above line will detect it.
> {
> fscanf(f,"%s",string);
No memory has been allocated for string, so it points somewhere random. Also, since the fscanf() return value isn't checked, we don't know what the status of string is for the next line (even if it did point to valid memory).
> if(!strcmp(string,word))//if match found
> number++:
> }
Thanx for the intricacies you have pointed out. I was just going to ask you for what problem you think is generated by gets(). The other thing is I never faced any prob with "uninitialised memory" as you say what is the probability of this thing giving an error.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.