Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to count words in a text file? 3

Status
Not open for further replies.

louize

Technical User
Apr 22, 2001
24
0
0
GB
please please please help!! how do i setup the program to read a text file that will count the number of a particular word in the file?
 
I'm at work so I need to be quick about this...

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.
 
#include <stdio.h>
void main()
{
char *word,*searchStr;
int no_of_word=0,i=0;
FILE *fp;
gets(searchStr);
fp = fopen(&quot;Xyz.txt&quot;,&quot;r&quot;);
while(fgetc(ch) != 'EOF');
{
*(word+i) = ch;
i++;
if(ch++ == ' ')
{
*(word+i) = '\0';
if(!strcmp(searchStr,word))
{
no_of_word++;
}
i=0;
}
}
printf(&quot;No. of time %s gets repeated is %d&quot;,searchStr,no_of_word);
}


Hope this code works. Compile it, may be it has some direct errors but u won't find any prob with logic.

SwapSawe.
 
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(&quot;Please enter the word to search):
scanf(&quot;%s&quot;,word);
f=fopen(&quot;filename.txt&quot;,&quot;r&quot;);
if (f==NULL)
{
perror(&quot;Can't open file&quot;);
exit(1);
}
while(!feof(f))
{
fscanf(f,&quot;%s&quot;,string);
if(!strcmp(string,word))//if match found
number++:
}
printf(&quot;We found the word %s in the file %d times&quot;,word,number);
return 0;
}

 
There are some problems with the above examples that should be pointed out.

>char *word,*searchStr;
>int no_of_word=0,i=0;
>FILE *fp;
>gets(searchStr);

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(&quot;Please enter the word to search):
>scanf(&quot;%s&quot;,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(&quot;%50s&quot;,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,&quot;%s&quot;,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++:
> }

Russ
bobbitts@hotmail.com
 
Hi robbitt,

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 &quot;uninitialised memory&quot; as you say what is the probability of this thing giving an error.

Thanx.
Regards,
SwapSawe.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top