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

This is a discussion on need help with 'character counting from a .txt file' within the C Programming forums, part of the General Programming Boards category; Hi, I am new here and I have a problem with my latest program writing-attempt. I hope someone corrects what ...

  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 01:21 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    fgets() reads from the file into a null-terminated string. The EOF is not stored. EOF is not even actually a character. It is an int so that its bit-pattern can be distinguished from all chars. The upshot of all this is that you need to check for a 0 instead of EOF.

  3. #3
    Registered User master_vst's Avatar
    Join Date
    Nov 2008
    Posts
    4
    Thank you very much!

    It worked..

    It is funny that it was such a stupid mistake (instead of stupid number )..

  4. #4
    Registered User master_vst's Avatar
    Join Date
    Nov 2008
    Posts
    4
    And I have another problem about this now..
    It worked only for simple texts..

    I actually wanted it for testing the forum-type texts where there are codes used..
    like ([/COLOR]), ( [ u ] [ / u ]) or I use these (------------------------------------------------------------) to make head lines look better..

    And when my program encounters with these it cant continue counting and just gives me the number untill these and finishes..

    How can I make them countable?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    I suspect you're only calling fgets() once, which means that you only read one line from the file. This is surely not what you want.

    If you're trying to read a file in line by line, and process each line individually, a common idiom goes something like this:
    Code:
    FILE *fp = /* ... */;
    char buffer[SIZE];
    while(fgets(buffer, sizeof(buffer), fp)) {
        /* do something with buffer */
    }
    In your case, it would be quite simple to count the characters in each line yourself. Something like this, which could be made shorter if you know about for loops:
    Code:
    int x = 0;
    while(buffer[x] != 0) {
        characters ++;
        x ++;
    }
    However, you're really just over-complicating the problem by reading in each line at a time. You could just read from the file character by character and be done with it.
    Code:
    FILE *fp = /* ... */;
    int count = 0;
    
    int c = getc(fp);
    while(c != EOF) {
        count ++;
        c = getc(fp);
    }
    
    printf("There are &#37;d characters in the file.\n", count);
    Something like that.

    (I've tried to write my code such that you should be able to understand it. Hopefully. )
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User master_vst's Avatar
    Join Date
    Nov 2008
    Posts
    4
    Ohh, I didnt knew that 'fgets()' reads only one line..

    I knew I made it over-complicated but I am new at programming and I was just trying to use what I already know..

    Thanks for the examples..

    I will try it with your version.. It looks easier.. and simple..

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, 09:46 AM
  5. read from .txt file & put into array?
    By slow brain in forum C Programming
    Replies: 6
    Last Post: 02-25-2003, 04:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21