Im trying to write a program which will count lines words and characters of a text file which can be read into the program
Any help would be much appreciated thanks
Im trying to write a program which will count lines words and characters of a text file which can be read into the program
Any help would be much appreciated thanks
Cheers
Bazza
1.) Total number of lines by counting total number of '\n' or '\r'.
2.) You increment a counter dedicated to 'words', everytime you encounter a white space (' ' or '\t').
3.) Total number of characters is the total number of characters in the file which qualify for a certain criteria based upon your perspective (isspace(), isalpha(), ...) of 'characters'.
If you still have trouble, post back with some sort of code, to show you've really worked on, because your question looks like a home work problem.
Hmm, I saw this program recently...Oh yes, it's in K&R and sprinkled all over the internet if you bother to do some work and find it. Or did you have a problem with your code that you forgot to ask? Because it sounded a bit like you wanted us to write it for you.
-Prelude
My best code is written with the delete key.
#include<ctype.h>
It's unfulfilled dreams that keep you alive.
//netboy
I didnt want you to do it for me thankyou I have worked on the code and managed to get a wordcount if you type a textfile called textfile.txt
The problem Im having now is when i do the character count it includes spaces and im sure how to stop the program reading theses spaces
An idea I have come up with is to have the characters counted and then - the number of words but i cant seem to calls a word count sub program within a character count subprogram and use the result to take the words from characters there must be a better way coz this feels like cheating.
[Code/]
void num_words(void)
{
{
FILE *stream;
char inword[MAX], *string[MAX];
int totalwords = 0;
stream = fopen("c:\\textfile.txt", "r");
/* If the file was not found, then exit. */
if (stream == NULL)
{
printf("\nError reading file.");
getchar() ;
getchar();
exit(1);
}
while(!feof(stream))
{
fscanf(stream, "%s", inword);
*string = inword;
totalwords++;
}
printf("\n%d total word(s)\n", totalwords);
getchar();
getchar();
getchar();
fclose(stream);
}
}
void num_chars(void)
{
{
FILE *stream;
char inchar[MAX], *string[MAX];
int totalchars = 0;
stream = fopen("c:\\textfile.txt", "r");
/* If the file was not found, then exit. */
if (stream == NULL)
{
printf("\nError reading file.");
getchar() ;
getchar();
exit(1);
}
while(!feof(stream))
{
fscanf(stream, "%c", inchar);
*string = inchar;
totalchars++;
printf("\n %d total character(s)\n", (totalchars-1));
getchar();
getchar();
fclose(stream);
}
}
Cheers
Bazza
A few pointers...
>while (!feof(stream))
Don't use feof() in this manner to determine that you have reached the end of the file. Instead, use the return code from the function that is actually doing the reading. The feof() method doesn't always work as you'd expect.
In your code, there are a few {'s in the wrong place, and the loop in num_chars() doesn't make much sense
To count all the chars in a file, I'd probably either use fgets() to fgetc() to load the data into memory. Then, determine if each character read is one I need to count, if so increment and move on.
I won't write the whole thing for you, here's a snippet that will do as I have suggested.
Hope this helps.Code:int c; while ((c = fgetc(fp)) != EOF) { if (c != ' ') CharCount++; }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]