-
Hit a brick wall here...
I think i have some mixed up stuff in my program, maybe some c++ in there as well. Used alot of internet sites and maybe misunderstood something. Im trying to get this thing to count the number of lines words and characters in a file name. any help would be appreciated. Here is what i have
Code:
#include<stdio.h>
void chars(struct file ); /* function prototype*/
void words(struct file ); /*function prototype*/
void lines(struct file ); /*function prototype*/
struct file;
struct in;
int main ()
{
file *in;
char fNAME[15]; /*limits chars to 15 for input*/
printf("\nEnter the file name: ");
scanf ("%s",fNAME);
in=fopen(fNAME, "r"); /*opens file*/
if(in == NULL) /*checks for file opening*/
{
printf("\n Cannot find file %s.\n",fNAME);
exit(1)
}
chars(in);
words(in);
lines(in);
fclose(in); /*fclose closes a file*/
system("pause");
return 0;
}
void chars(file * fNAME) /*a pointer to a file*/
{
long ftell();
fseek(fNAME, OL, SEEK_END); /*for file positioning to the end of the file*/
printf("The number of characters in the file is: %d\n",ftell(fNAME));
return;
}
void words(filw * fNAME) /*a pointer to a file*/
{
long ftell();
fseek(fNAME, OL, SEEK_END);/*for file positioning to the end of the file*/
printf("The number of words in the file is: %d\n",ftell(fNAME));
return;
}
void lines(file * fNAME) /*a pointer to a file*/
{
long ftell();
fseek(fNAME, OL, SEEK_END); /*for file positioning to the end of the file*/
printf("The number of lines in the file is: %c\n",fNAME);
return;
}
-
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. :D
-
fgetc returns int
so generaly using of fgetc is like
Code:
int c;
while((c = fgetc(file)) != EOF)
{
/* do something with c */
}
instead of manual checking of the diapasons like 'a':'z' better to use function isupper and islower - they are portable.
for the space checking isspace can help