Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

word count

690 views
Skip to first unread message

lisa eldred

unread,
Apr 15, 2002, 3:14:19 PM4/15/02
to
im trying to write a program which has the user speicfy the file name and
the runs a count of all the words within it. The code i have wrote however
doesnt seem to work any ideas what im doing wrong?

#include <stdio.h>
#include <stdlib.h>
#define MAX_LENGTH 256

main()
{
FILE *fp;
char ch, filename[40];
int word_count=0;
char inword[MAX_LENGTH];


/*get file name and open*/

while(1)
{

printf("\nEnter a filename: ");
gets(filename);

/*check if the file is valid*/

if( (fp = fopen(filename, "r" )) !=NULL )
{
printf("\n Succesfully opening %s. \n", filename);

}
else
{
fprintf(stderr, "\n error opening file %s.\n", filename);
puts("Enter x to exit, any other to try again.");
if( (ch=getch()) == 'x' )
break;
else
continue;
}
}

/*count the words in the file*/

while (!feof(filename) )
fscanf( filename, "%s", inword);
word_count++;
}

/* display amount of words*/

printf("there are %d word(s) in the file %s\n", word_count, filename);

/*close file*/

fclose(filename);

return(0);

}

David Rubin

unread,
Apr 15, 2002, 3:55:51 PM4/15/02
to
lisa eldred wrote:
>
> im trying to write a program which has the user speicfy the file name and
> the runs a count of all the words within it. The code i have wrote however
> doesnt seem to work any ideas what im doing wrong?

> #include <stdio.h>
> #include <stdlib.h>
> #define MAX_LENGTH 256

> main()

int
main(void)

> {
> FILE *fp;
> char ch, filename[40];

^^^^^^^^

Should be

char filename[40];
int ch;

to accommodate EOF.

> int word_count=0;
> char inword[MAX_LENGTH];
>
> /*get file name and open*/
>
> while(1)
> {
>
> printf("\nEnter a filename: ");

printf("Enter a filename: ");
fflush(stdout);

> gets(filename);

fgets(filename, sizeof filename, stdin);



> /*check if the file is valid*/
>
> if( (fp = fopen(filename, "r" )) !=NULL )
> {
> printf("\n Succesfully opening %s. \n", filename);

You don't really need to know this except perhaps for debugging
purposes. Otherwise, you don't care.

> }
> else
> {
> fprintf(stderr, "\n error opening file %s.\n", filename);
> puts("Enter x to exit, any other to try again.");
> if( (ch=getch()) == 'x' )
> break;
> else
> continue;

How about

printf("Hit [Return] to try again...");
fflush(stdout);
getchar();

If the user doesn't want to continue, she can enter EOF (possibly ^D or
^Z) or close the application. Getch is not a standard function.

> }
> }
>
> /*count the words in the file*/
>
> while (!feof(filename) )

You're missing an open-brace here, but feof is not the correct loop
condition, nor is filename a FILE*.

> fscanf( filename, "%s", inword);
> word_count++;
> }

This should be

char *delims=" \t\n"; /* word delimiters */

or something sufficient, defined at the top of main, and

while(fgets(inword, sizeof inword, fp) != 0){
char *p;
for(p=strtok(inword, delims); p != 0; p=strtok(0, delims))
word_count++;
}

Alternatively, you can search character-by-character and use the is*
functions from ctype.h to search for words.

> /* display amount of words*/

> printf("there are %d word(s) in the file %s\n", word_count, filename);

> /*close file*/

> fclose(filename);

fclose(fp);

> return(0);

return 0; /* parentheses not needed */

> }

--
If 91 were prime, it would be a counterexample to your conjecture.
-- Bruce Wheeler

Eric Sosman

unread,
Apr 15, 2002, 4:34:06 PM4/15/02
to
lisa eldred wrote:
>
> im trying to write a program which has the user speicfy the file name and
> the runs a count of all the words within it. The code i have wrote however
> doesnt seem to work any ideas what im doing wrong?
> [...]
> while (!feof(filename) )
> [...]

Here's your problem, and it's the subject of Question 12.2
in the comp.lang.c Frequently Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Eric....@sun.com

Bob Wightman

unread,
Apr 15, 2002, 5:26:26 PM4/15/02
to
In message <lEFu8.22275$tZ1.6...@news2-win.server.ntlworld.com>, lisa
eldred <lisae...@hotmail.com> writes

>im trying to write a program which has the user speicfy the file name and
>the runs a count of all the words within it. The code i have wrote however
>doesnt seem to work any ideas what im doing wrong?
>
Judging from the number of obvious windups this could be a troll,
however....

>#include <stdio.h>
>#include <stdlib.h>
>#define MAX_LENGTH 256
>
>main()
Should be int main(void)

>{
>FILE *fp;
>char ch, filename[40];
Getchar returns an int not a char.

>int word_count=0;
>char inword[MAX_LENGTH];
>
>
>/*get file name and open*/
>
>while(1)
>{
>
>printf("\nEnter a filename: ");
>gets(filename);
Never use gets, it is a bug waiting to happen, use fgets(filename, 40,
stdin) instead

>
>/*check if the file is valid*/
>
>if( (fp = fopen(filename, "r" )) !=NULL )
>{
>printf("\n Succesfully opening %s. \n", filename);
>
>}
>else
>{
>fprintf(stderr, "\n error opening file %s.\n", filename);
>puts("Enter x to exit, any other to try again.");
>if( (ch=getch()) == 'x' )
Getch is a non-standard function, though it is obvious to those who have
programmed with DOS implementations of C what you are doing. Again
fgets is the function to use.

>break;
>else
>continue;
>}
>}
>
>/*count the words in the file*/
>
>while (!feof(filename) )
This doesn't do what you think it does, the above construct will read
the last line in the file twice.

>fscanf( filename, "%s", inword);
>word_count++;
>}
>
>/* display amount of words*/
>
>printf("there are %d word(s) in the file %s\n", word_count, filename);
>
>/*close file*/
>
>fclose(filename);
>
>return(0);
>
>}
>
>
>
The following does what you want

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define MAXCHAR 250
int main(void)
{
int c; /* for return value of fgetc */
int inword = 0;
FILE *fp;
char filename[MAXCHAR];
unsigned int count = 0;

printf("Enter the name of the file to open\n");
fgets(filename, MAXCHAR, stdin);
filename[strlen(filename) - 1] = '\0';

if (!(fp = fopen(filename, "r")))
{
printf("Unable to open %s\n", filename);
exit(EXIT_FAILURE);
}

while((c = fgetc(fp)) != EOF)
{
if(isalpha((unsigned char)c))
{
inword = 1;
}
else
{
if(inword)
{
inword = 0;
++count;
}
}
}

fclose(fp);

printf("There were %u words in %s\n", count, filename);
return 0;
}

--

Change the gender of the child to reply.
b...@pattindaughter.demon.co.uk

Bob Wightman

David Rubin

unread,
Apr 16, 2002, 10:41:38 AM4/16/02
to
Bob Wightman wrote:
>
> In message <lEFu8.22275$tZ1.6...@news2-win.server.ntlworld.com>, lisa
> eldred <lisae...@hotmail.com> writes
> >im trying to write a program which has the user speicfy the file name and
> >the runs a count of all the words within it. The code i have wrote however
> >doesnt seem to work any ideas what im doing wrong?

[snip]


> The following does what you want

[snip]


> while((c = fgetc(fp)) != EOF)
> {
> if(isalpha((unsigned char)c))
> {
> inword = 1;
> }


Your definition of a word is not all that liberal. I suggest comparing
your results to wc(1) as a point of reference. In particular,
words like "don't", "e-mail", and "UCHAR_MAX" are all counted as two
words.

david

Bob Wightman

unread,
Apr 17, 2002, 2:29:06 AM4/17/02
to
In message <3CBC3822...@hotmail.com>, David Rubin
<dlr...@hotmail.com> writes

>Bob Wightman wrote:
>>
>> In message <lEFu8.22275$tZ1.6...@news2-win.server.ntlworld.com>, lisa
>> eldred <lisae...@hotmail.com> writes
>> >im trying to write a program which has the user speicfy the file name and
>> >the runs a count of all the words within it. The code i have wrote however
>> >doesnt seem to work any ideas what im doing wrong?
>
>[snip]
>> The following does what you want
>
>[snip]
>> while((c = fgetc(fp)) != EOF)
>> {
>> if(isalpha((unsigned char)c))
>> {
>> inword = 1;
>> }
>
>
>Your definition of a word is not all that liberal. I suggest comparing
>your results to wc(1) as a point of reference. In particular,
>words like "don't", "e-mail", and "UCHAR_MAX" are all counted as two
>words.

True, I should have used isspace to determine the transition to white
space rather than isalpha. I'll leave that to the OP as an exercise in
program maintenance :-)

0 new messages