There's somehow a good amout of syntax/semantic error in your code. It would take some time to explain them all, especially since english isn't my first language hah. But i'm sure people will help you (or, at least, they'll redirect you to good source of information).
I'll just talk to you about your 3 functions, chars(..), words(..) and lines(..).
I don't know if you realize, but they are all the same (except the last one who has a bigger number of errors heh..). And, well, you should understand that counting the number of caracter isn't the same thing as counting the number of words nor the number of lines.
Your function chars(FILE *file) is the closer one to be correct. Without the errors, it should looks like this :
Code:
void chars(FILE *file) /*a pointer to a file*/
{
fseek(file, 0, SEEK_END); /*for file positioning to the end of the file*/
printf("The number of characters in the file is: %dl\n", ftell(file));
return;
}
It would give the correct answer depending on what is your definition of a character. (are non-printable characters are characters for you ?)
Next, second easiest function, lines(FILE *file). Well, for this, you only need to count the number of newline character ('\n') from the beginning of the file. Should looks like this:
Code:
void lines(FILE *file)
{
int linesNb = 1;
if (file != NULL)
{
rewind(file); // making sure we are at the beginning of the file
while (!feof(file))
{
if (fgetc(file) == '\n')
{
// Here's a new line character
linesNb = linesNb + 1;
}
}
printf("The number of lines in the file is: %d\n", linesNb);
}
return;
}
And for the words counting function... well... it should look for an alphabetic character, then increment the number of words, and then moving to the next whitespace and doing this for all the file. It's going to be appropriate in most of the case. Here's an example
Code:
void words(FILE *file)
{
int wordsNb = 0;
char car;
if (file != NULL)
{
rewind(file);
while (!feof(file))
{
do
{
car = fgetc(file);
} while ((!feof(file)) && ((car < 'a') || (car > 'z')) && ((car < 'A') || (car > 'Z')));
if (!feof(file))
{
/* A: car is an alphabetic caracter */
wordsNb = wordsNb + 1;
do
{
car = fgetc(file);
}
while ((!feof(file)) && (car != ' ') && (car != '\n') && (car != '.'));
}
}
printf("The number of words in the file is: %d\n", wordsNb);
}
return;
}
That's it for today.