> Now, if i count character one by one, it is should be 20 ( 5 chars for Hello + 8 chars for Everyone and 7 chars for Testing)
You are also counting \0 and \n for every line.
Main Topics
Browse All TopicsHi,
I have written a small program to gain understanding to how file opens works..
The program counts the number of characters and lines in the file...
I found it strange as in the test.txt file I had the following:
Hello
Everyone
Testing
And the number of counts of characters i got was : 26
And the number of new lines i got was: 0
Now, if i count character one by one, it is should be 20 ( 5 chars for Hello + 8 chars for Everyone and 7 chars for Testing)
Now,if i count how many new lines i got , it should be 3:
What is could go wrong :-(
int countChar(char *,int *)
int main()
{
int c;
int *lCount;
lCount=malloc(sizeof(int))
c=countChar("test.txt",lCo
printf("The number of char counts : %d\n",c);
printf("The number of new lines : %d\n",*lCount);
return 0;
}
int countChar(char *f,int *lCount)
{
int count=0;
FILE *fp;
int ch;
fp=fopen(f,"r+");
if(fp==NULL)
return -1;
while((ch=fgetc(fp))!=EOF)
{
if(ch=='\n')
*lCount++;
count++;
}
return count;
}
Also, is there any function in C that allows to count word . i.e Hello - 1 word Everyone - 1 word
thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi,
Thanks a lot
I initialized *lCount in the main function...
Also, I changed to (*lCount)++ in cc function...
But now, for counts which involves characters only, I am getting 25 ...(Since all the word would have '\0' and new line, i thnk it should be 26...) .This is so confusing :-(
But for the lines, I am getting correct answer which is 3..
Hello
Everyone
Testing
Forget about the '\0' ... There are no '\0' in text files. The extra characters counted were all '\n' (or possibly '\r' or extra spaces or ...).
To find out which characters were read, you can write each character once you read it like this :
printf("%02x ", ch); /* <--- put this in the while loop */
If you want, you can paste your program's output here, and we can tell you why it counts 25 ...
And here's the corrected code :
int countChar(char *, int *); /* <--- you forgot a ; at the end of this line */
int main() {
int c = 0; /* <--- it's always good to initialize your variables once you declare them */
int lCount = 0; /* <--- don't make this a pointer - you don't need to (the malloc line can then go too) */
c = countChar("test.txt", &lCount); /* <--- and instead we pass the address of lCount */
printf("The number of char counts : %d\n", c);
printf("The number of new lines : %d\n", lCount); /* <--- we can simply show lCount now ... */
return 0;
}
int countChar(char *f, int *lCount) {
int count = 0;
FILE *fp = 0;
int ch = '\0';
fp = fopen(f, "r+");
if (fp == NULL)
return -1;
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n')
(*lCount)++; /* <--- see ozo's comments */
count++;
}
return count;
}
Note that I indented your code to make it better readable. I commented the modifications I made. Some are to make your life easier (like the malloc), some are to avoid bugs (like initializing the variables once they are declared), and others are to fix errors (like the fix already made by ozo).
Business Accounts
Answer for Membership
by: ozoPosted on 2007-05-27 at 22:33:45ID: 19166192
(*lCount)++;