Unsuccessful Attempt to Count Words and Bytes in a File

This is a discussion on Unsuccessful Attempt to Count Words and Bytes in a File within the C Programming forums, part of the General Programming Boards category; The task is to count words, bytes and lines in a file.txt. My program does not work because terminal reads: ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    1

    Unsuccessful Attempt to Count Words and Bytes in a File

    The task is to count words, bytes and lines in a file.txt.
    My program does not work because terminal reads:

    bash-4.1$ gcc main.c functions.c -o space
    bash-4.1$ ./space maggie.txt
    maggie.txt LINES:1 WORDS: 5 BYTES:10

    maggie.txt file:
    euler jdk

    terminal is supposed to read:
    maggie.txt LINES:1 WORDS:2 BYTES:9

    Code:
    #include <stdio.h>
    #include "functions.h"
     
    /*
     *Maggie Cao
     *CS102E Fall 2012
     *Project 10
     *November 26, 2012
     *Noah Santacruz helped me with Lab 5
    */
     
     
     
    int wc ( char* fileName ) /*void b/c not giving any info back*/
    {
            FILE* wc= fopen (fileName, "r");
            char buffer [1024]; //**only read a junk of info not everything**//
                    if (wc != NULL){
                    int i=0;
                    int numChars=0;
                    int numSpace=0;
                            while (fgets(buffer, 1024, wc) != NULL){
                                    numChars= strLength(buffer) + numChars;
                                    i++;
                                    numSpace=strSpace(buffer) +numSpace;
                            }
                    fprintf(stdout, "%s LINES:%d WORDS:%d BYTES: %d\n",fileName,i,numSpace,numChars);
                    fclose(wc);
                    }
                    else {fprintf (stdout, "error %s failed to read", fileName);
                    }
                    return 0;
                    }
     
    int strLength (char* string) {
            int strLength=0;
            int isNullChar=0;
                    while (isNullChar==0){
                            char currentChar=string[strLength];
                            if (currentChar== '\0'){
                                    isNullChar=1;
                            }
                            else { strLength=strLength+1;
                            }
                    }
            return strLength;
    }
     
    int strSpace (char* text){
            int strSpace=0;
            int isNullChar=0; /*can be the same as strLength*/
                    while (isNullChar==0){
                            char currentChar=text[strSpace];
                            if (currentChar == ' ' || currentChar== '\0'){
                                    isNullChar=1;
                            }
                            else {strSpace=strSpace+1;
                            }
                    }
            return strSpace;
    }
    Please help!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Would you consider an iterative, rather than a recursive solution?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,276
    There is no need for your strLength or strSpace functions (strLength, afterall, is just a suboptimal implementation of the built-in strlen() function). You would be better off reading the file character by character, using fgetc. Remember, fgetc returns an int, not a char, so declare the variable that holds it's result as an int. Keep a counter for each of: numLines, numWords and numBytes. Every character you read is a byte, so increment byte every time, regardless. Every time you read a newline character ('\n'), increment your new line count.

    Counting words is the only one that is kind of tricky. You basically need to know when you "leave" a word. That is, when you reach the end of one, or when you go from an "in word" character (letter, number perhaps, and maybe punctuation), to a "non-word" character, like a space, tab, newline. Keep a state variable (called something useful like inWord). Initialize it to 0 (before you read any chars, you do not have a word -- your file could be all spaces). Spend a bit of time trying to figure out the logic for word counting, it's good practice for you (and you would learn nothing if I gave you the whole answer). If you get stuck again, post back and we will help you. Also, look in the ctype.h header, there are useful functions like isspace, isalphanum, ispunct, etc.

    EDIT: Note, you will have problems if you use 'wc' as the name of a function and a variable. Change the name of one of those, and all the places you use it. You also do this with strLength and strSpace, which you wont need anymore. Note, C is not like Pascal, so assigning a value to the name of the function does not set the return value. You need a separate variable to track the value, and return that.
    Last edited by anduril462; 01-15-2013 at 01:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-16-2012, 12:51 PM
  2. Replies: 1
    Last Post: 04-27-2011, 10:56 PM
  3. count words
    By 74466 in forum C++ Programming
    Replies: 4
    Last Post: 02-17-2006, 08:30 AM
  4. words count
    By arlenagha in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2003, 08:29 AM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM

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