Thread: need help with 'character counting from a .txt file'

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User master_vst's Avatar
    Join Date
    Nov 2008
    Posts
    4

    Question 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 = &#37;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)
    Last edited by master_vst; 11-09-2008 at 02:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 05:16 AM

Tags for this Thread