So basically I'm a little bit confused. I believe the logic of my program is correct; file pointer is declared, and I do have a character to store my array, it reads the file, and counts all the words pending a space in front/between/etc. Basically the program should ask the used the used to input the name of the file they're scanning. The file opens and goes through the program and outputs a line that tells the user the # of words in the file that was inputted.
My problem has been compiling it.
How can it possibly do all that stuff you claim it does if the program doesn't even compile? The mind boggles! :icon_rolleyes:
Your code:
#include <stdio.h>
#include <string.h>
int main()
{
int count = 0;
char *doc; // Where is the actual storage? A pointer
// does not hold data, it holds a pointer
FILE *inp;
printf("Enter the file name: ");
scanf("%s", doc); // See the scanf() link below
inp=fopen(doc, "r"); // Why are you opening the file with a char*?
// Aren't you supposed to use a FILE pointer?
while((doc = fgetc(doc)) != EOF) // You get a character and
// 1) destroy your file pointer
// 2) destroy your character pointer.
{
if (doc == ' ') // doc is a pointer. It can't be == to a character.
count++;
}
fclose(doc);
printf("%s contains %d words\n",doc,count);
return 0;
} WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944