need help with 'character counting from a .txt file'
Hi,
I am new here and I have a problem with my latest program writing-attempt.
I hope someone corrects what I have done wrong..
The question is that I was writing a message on a online text-based game and I realized that they allowed max 20000 characters to send.
Inspired from this I wanted to make program that counts the chracters (up to 25000) of whatever I wrote (or copy-pasted) in a certain .txt file.
Then I programmed this:
-----------------------------------------------------------
Code:
#include <stdio.h>
int main ()
{
FILE *body;
char content[25000];
long c = 0;
long int s;
/* First, I get the textbody into a flow named 'body'. */
/* Then I get the contents into an array named 'content'. */
body = fopen("C:\\Documents and Settings\\USER\\Desktop\\char_counter\\body.txt", "r");
fgets(content, 25000, body);
while (content[c] != EOF) /* Then I increase c every time at 1 before the EndOfFile to get the number of the characters. */
{
c = c + 1;
}
printf("number of all characters = %ld\n\n", c);
scanf("%lf", &s); /* I put this here because somehow console closes itself and I can't read anything.*/
return 0;
}
-----------------------------------------------------------
Then I wrote "master" in the file and expected to see '6' but I got '19748' !
I thought that windows adds something at the end of my file so that I cant take the true value. I changed the program into this and tried to manually decrease that value into expected values:
-----------------------------------------------------------
Code:
#include <stdio.h>
int main ()
{
FILE *body;
char content[44742]; /* 25k + stupid */
long stupid = 19742; /* stupid number added by windows (I think) */
long c = 0;
long b = 0;
long int s;
/* First, I get the textbody into a flow named 'body'. */
/* Then I get the contents into an array named 'content'. */
body = fopen("C:\\Documents and Settings\\USER\\Desktop\\char_counter\\body.txt", "r");
fgets(content, 44742, body);
while (content[c] != EOF) /* Then I increase c every time at 1 before the EndOfFile. */
{
c = c + 1;
}
c = c - stupid; /* When there are 19742 characters more than the expected value then I just decrease them to reach the expected value.. */
printf("number of all characters = %ld\n\n", c);
while (b < c) /* I am trying to see which characters are these '19742' characters.. :S */
{
printf("%c", content[b]);
b = b + 1;
}
scanf("%lf", &s); /* I put this here because somehow console closes itself and I can't read anything.*/
return 0;
}
-----------------------------------------------------------
The result was not better :(.
"number of all characters = 19750"
"master "
" "
" "
(It goes down like more than 250 - 300 lines.. I couldnt count :S..)
I hope I am not boring anyone with these codes and stuff please help me...
I need help to correct it. I have made lots of searches but I still keep getting lost..
I really want this thing work..
(I am using Dev-C++ on Windows XP. and notebook for .txt :))
thanks,
volkan
(master_vst)