#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);
}
> #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
Here's your problem, and it's the subject of Question 12.2
in the comp.lang.c Frequently Asked Questions (FAQ) list
#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
[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
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 :-)