1

It's supposed to emulate the WC command, but I can't seem to get my readFile method working. I think it has something to do with pointers, but I'm pretty new to C and I still don't quite understand them. Thank you so much for your help! Here's the source code:

/*
 *  This program is made to imitate the Unix command 'wc.'
 *  It counts the number of lines, words, and characters (bytes) in the file(s) provided.
 */

#include <stdio.h>

int main (int argc, char *argv[])
{
    int lines = 0;
    int words = 0;
    int character = 0;
    char* file;
    int *l = &lines;
    int *w = &words;
    int *c = &character;

    if (argc < 2) //Insufficient number of arguments given.
        printf("usage: mywc <filename1> <filename2> <filename3> ...");
    else if (argc == 2)
    {
        file = argv[1];
        if (readFile(file, l, w, c) == 1)
        {
            printf("lines=%d\t words=%d\t characters=%d\t file=%s\n",lines,words,character,file);
        }
    }
    else
    {
        //THIS PART IS NOT FINISHED. PAY NO MIND.
        //int i;
        //for(i=1; i <= argc; i++)
        //{
        //    readFile(file, lines, words, character);
        //}
    }
}

int readFile(char *file, int *lines, int *words, int *character)
{
    FILE *fp = fopen(file, "r");
    int ch;
    int space = 1;

    if(fp == 0)
    {
        printf("Could not open file\n");
        return 0;
    }

    ch = fgetc(fp);

    while(!feof(fp))
    {
        character++;
        if(ch == ' ') //If char is a space
        {
            space == 1;
        }
        else if(ch == '\n')
        {
            lines++;
            space = 1;
        }
        else
        {
            if(space == 1)
                words++;
           space = 0;
        }
        ch = fgetc(fp);
    }
    fclose(fp);
    return 1;
}
  • What does the debugger tell you? Or else, good old `printf()` statements? – meaning-matters Feb 05 '14 at 16:36
  • the printf() statement in main (printf("lines=%d...etc)) is returning all zeros. im adding in some print statement now to try and figure out where the problem could be – user2766421 Feb 05 '14 at 16:40

2 Answers2

0

In your readFile function you are passing in pointers. So when you increment lines, you are incrementing the pointer, not the value it points to. Use the syntax:

*lines++;

That de-references the pointer and increments the value it points to. Same with words and character.

Chris J. Kiick
  • 1,487
  • 11
  • 14
0

First, you may have problems with while(!feof(fp)). It is not often a recommended method and should always be used with care.

EDITED with new function:

Here is another example of how you can get number of words in a file:

int longestWord(char *file, int *nWords)
{
    FILE *fp;
    int cnt=0, longest=0, numWords=0;
    char c;
    fp = fopen(file, "r");
    if(fp)
    {
        while ( (c = fgetc ( fp) ) != EOF )
        {
            if ( isalnum ( c ) ) cnt++;
            else if ( ( ispunct ( c ) ) || ( isspace ( c ) ) )
            {
                (cnt > longest) ? (longest = cnt, cnt=0) : (cnt=0);
                numWords++;
            }
        }
        *nWords = numWords;
        fclose(fp);
    }
    else return -1;

    return longest+1;
}

Note: This also returns the longest word, which can be useful when determining how much space to allocate if and when you need to place all of the words of a file into an array of strings, for example.

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87