Reputation: 795
This is the code:
#include <stdio.h>
int main(void)
{
char words[256];
char filename[64];
int count = 0;
printf("Enter the file name: ");
scanf("%s", filename);
FILE *fileptr;
fileptr = fopen(filename, "r");
if(fileptr == NULL)
printf("File not found!\n");
while ((fscanf(fileptr, " %s ", words))> 0)
{
if (words==' ' || words == '\n')
count++;
}
printf("%s contains %d words.\n", filename, count);
return 0;
}
I keep getting this error:
warning: comparison between pointer and integer [enabled by default]
if (words==' ' || words == '\n')
^
I don't get the error once I change, words
to *words
but that does not give me the correct results. I am trying count the number of words in a file.
Upvotes: 0
Views: 259
Reputation: 9680
You cannot compare strings in C
. You should compare them character by character using the standard library function strcmp
. Here's its prototype contained in the string.h
header.
int strcmp(const char *s1, const char *s2);
The strcmp
function compares the two strings s1
and s2
. It returns an integer less than, equal to, or greater than zero if s1
is found, respectively, to be less than, to match, or be greater than s2
.
The format string of fscanf " %s "
(note the trailing and the leading space) will read and discard any number of whitespaces which it does anyway with the format string "%s"
. This means no whitespaces will be written into the buffer words
by fscanf
. fscanf
will write only non-whitespace characters in words
and returns when it encounters a whitespace. So, to count the number of words, just increase the counter for each successful fscanf
call.
Also, your program should check for possible buffer overflow in scanf
and fscanf
calls. If the input string is too big for the buffer, then this would cause undefined behaviour and even causing crash due to segfault. You can guard against it by changing the format string. scanf("%63s", filename);
means scanf will read from stdin
until it encounters a whitespace and write at most 63
non-whitespace characters in the buffer filename
and then add a terminating null byte at the end.
#include <stdio.h>
#include <string.h>
int main(void) {
// assuming max word length is 256
// +1 for the terminating null byte added by scanf
char words[256 + 1];
// assuming max file name length is 64
// +1 for the terminating null byte
char filename[64 + 1];
int count = 0; // counter for number of words
printf("Enter the file name: ");
scanf("%64s", filename);
FILE *fileptr;
fileptr = fopen(filename, "r");
if(fileptr == NULL)
printf("File not found!\n");
while((fscanf(fileptr, "%256s", words)) == 1)
count++;
printf("%s contains %d words.\n", filename, count);
return 0;
}
Upvotes: 0
Reputation: 40145
not necessary compare because %s
(words
) does not contain white spaces(e.g. ' '
or '\n'
).
try this
while (fscanf(fileptr, "%s", words)> 0) {
count++;
}
Upvotes: 1
Reputation: 7290
Take care that using the array name words by itself implies a pointer to the first element in the array. If what you need is to compare 2 strings in C then the strcmp is what you are looking for.
Upvotes: 0
Reputation: 2898
words is char pointer while ' '
is char, *words equals to words[0]
usually we would define a new pointer as below
char *p = words;
while(*p != '\0' )
{
// using *p something you need to do
p++;
}
Upvotes: 1