-1

I know there are several asks already regarding this topic, but it seemed like those were either not completely answered or hard to apply to my code, so I apologize if this is a repeat. I am having trouble with the below function in an overall I/O program that also does word and line count (those work). char* filename is pulled from the command line. In this example it is pulling from a txt file with lorum ipsum. (69 words) In theory the below function should read from filename and write it to an array. Then read that array and checks if the current character is a space ' ' and the next character is not. It currently returns 0 regardless.

int wordcount(char* filename) {
int wc=0,i=0,z=0;
char w, test[1000];
FILE *fp;
fp = fopen(filename, "r");
        while (feof(fp) == 0) {
                fscanf(fp, "%c", &test[i]);
                        i++;
        }
        while (z>i-1) {
                if (test[z] = ' ' && test[z+1] != ' ' ) {

                wc++;z++;
        }
        }
return wc;
}

NOTES: i know it's super inefficient to declare a 1000 char array, but I wasn't sure how else to do it. If you have any improvements or other methods to accomplish this, it would be greatly appreciated if you shared. Also, i'm aware that this currently ignores others types of whitespace, but I am just testing this first and will expand after.

Thanks for any assistance.

Arkarian
  • 25
  • 6
  • https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong (for a start) – wildplasser Feb 05 '18 at 00:28
  • Thank you! I have no idea why we would be taught to use feof and then correct the output post-humorously instead of just using the correct method instead. /sigh – Arkarian Feb 05 '18 at 00:51
  • 'i know it's super inefficient to declare a 1000 char array,' you should limit the chars read by the fscanf to 999, just in case, but don't be afraid of over-sizing arrays. Every few days, we get a bean-counter who has laboriously tried to save every byte and so ends up declaring arrays too small, forgetting about the NUL terminator etc. – Martin James Feb 05 '18 at 06:58

2 Answers2

0

The concrete problem why you always get a result of 0 words:

while (z>i-1) {

z is never larger than i-1. Probably you meant to loop while z is smaller than i-1 instead:

while (z<i-1) {

Additionally you only increment z when you find a word. You should increment it for every character you test, no matter if it's a space or not.

sth
  • 222,467
  • 53
  • 283
  • 367
  • Thank you! I've flipped the operator and moved the wc++ outside of the while loop. However, while I now receive an output from the function it is a very large number, much larger than anticipated. Adding 'printf("Returning number %d", wc); ' right before closing the function returns 384.Characters total to 451, so it is limiting it somewhat, but not nearly to where it needs to be.. Any idea what could be going on? – Arkarian Feb 05 '18 at 00:59
0

There is a sample function doing what you need. Some suggestions for you code, fopen() must be followed by fclose() when you no longer need the file. Always check if the pointer returned by fopen is not NULL and do nothing in that case, just return error code. The presence of new word can be safely detected by the space character followed by a non space character in that case increment world count ++wc. Use getc() to read one character from the file object and use isspace() function to check if the character is a space one. You don't need an array to store the file if no one modifies that file during the worldcount run.

int wordcount(const char* filename)
{
    int wc=0;
    char c;
    FILE *fp;

    fp = fopen(filename, "r");
    if(fp == NULL)
    {
        return -1;
    }

    bool previsspace = 1;
    while ((c=getc(fp)) != EOF)
    {
        if (isspace(c)==0 && (previsspace==1))  ++wc;
        previsspace = isspace(c);
    }
    fclose(fp);
    return wc;
};

You will need the following include files:

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
Arkarian
  • 25
  • 6
  • Thanks, this worked perfectly! Out of curiosity why is `++wc; previsspace = isspace(c);` not wrapped in brackets? – Arkarian Feb 05 '18 at 01:45
  • Because I was lazy adding the brackets for the if condition and decided to put ++wc; on the same line with the if. Please notice previsspace = isspace(c); is outside the if condition – Tincu Stefan Lucian Feb 05 '18 at 02:02
  • Oh, neat. Sorry I wasn't aware the syntax allowed for that. – Arkarian Feb 05 '18 at 02:41