Thread: Auto count

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    28

    Talking Auto count

    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

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    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.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  5. #5
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158
    #include<ctype.h>
    It's unfulfilled dreams that keep you alive.

    //netboy

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    28
    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

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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.
    Code:
    int c;
    while ((c = fgetc(fp)) != EOF)
    {
    	if (c != ' ') CharCount++;
    }
    Hope this helps.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  2. bintree and count (withouth using template)?
    By cubimongoloid in forum C++ Programming
    Replies: 7
    Last Post: 05-24-2009, 06:22 AM
  3. input question
    By piyush_v in forum C Programming
    Replies: 9
    Last Post: 04-12-2007, 07:09 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM