Home > Software engineering >  Function is not correctly incrementing variable by 1
Function is not correctly incrementing variable by 1

Time:10-11

I have a function that returns the number of lines, characters, and words in an array. For some reason, when i loop through the array to print the values I am only getting the corrrect value for lines, the characters and words are returning as 0. All the functions are predetermined by my professor and my job is to fill them in.

int main(int argc, char **argv)
{
        int *myArray = get_counts(argv[1]);
        for (int i = 0; i < 3; i  )
        {
                printf("%d\n", myArray[i]);
        }
        return 0;
}
int *get_counts(char *filename)
{
        FILE *file;

        file = fopen(filename, "r");

        if (file == NULL)
        {
                printf("NULL FILE");
        }

        char c;
        int h;
        bool whitespace = true;
        static int arr[3] = {0,0,0};

        do
        {
                c = fgetc(file);
                if (c == '\n')
                {
                        arr[0]   ;
                }
        }while (c != EOF);

        while (true)
        {
                h = fgetc(file);

                if (feof(file))
                {
                        break;
                }
                else if (ferror(file))
                {
                        printf("error reading file");
                }
                arr[2]   ;

                if (whitespace && !isspace(h))
                {
                        arr[1]   ;
                        whitespace = false;
                }
                else if (!whitespace &&isspace(h))
                {
                        whitespace = true;
                }
        }
        fclose(file);
        return arr;
}

CodePudding user response:

The best option is probably to just iterate through the file in one loop (you could also rewind() after the first loop). Use the return value of fgetc() to determine of you are at EOF instead of separate feof() calls. I also made the the result array an (out) argument instead of using a static variable (the latter is not reentrant if you ever want to call this from multiple threads and it's easy to do):

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

void get_counts(char *filename, int arr[3]) {
    memset(arr, 0, 3 * sizeof(int));
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        printf("NULL FILE");
        return;
    }
    bool whitespace = true;
    for(;;) {
        int c = fgetc(file);
        if(c == EOF)
            break;
        else if(c == '\n')
            arr[0]  ;
        else if (whitespace && !isspace(c)) {
            arr[1]  ;
            whitespace = false;
        } else if (!whitespace && isspace(c))
            whitespace = true;
        arr[2]  ;
    }
    fclose(file);
}

int main(int argc, char **argv) {
    int myArray[3];
    get_counts(argv[1], myArray);
    for (int i = 0; i < 3; i  ) {
        printf("%d\n", myArray[i]);
    }
}

The output on the above file is:

39
94
715

The word count 94 doesn't agree with wc -w but you could be using a different definition of what a word is.

It's a good idea to separate calculations and i/o, so consider opening and closing the file in main() and pass in the file handle. It becomes easy, for instance, to use the stdin file handle instead if you don't want to use a physical file.

CodePudding user response:

After the first do-while loop the condition EOF occurs.

    do
    {
            c = fgetc(file);
            if (c == '\n')
            {
                    arr[0]   ;
            }
    }while (c != EOF);

So the following while loop has no effect.

You should use only one loop to count lines, words and characters.

Pay attention to that the variable c should be declared as having the type int

int c;

Also you need to exit the function if the file was not opened.

  • Related