Nuggets10 Nuggets10 - 11 days ago 5
C Question

C: Sorting order from file input, receiving an abundance of compilation error

This is my first time working with file input and pointers so I'm sorry if my code seems like a mess. I was looking at other Stack Overflow solutions as a reference. My code is meant to find the longest word in a file where I can assume that the input text will not have a word longer than 1000 characters.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// main() must return an integer
int main(int argc, char** argv) {
FILE *file; //open an existing file
file = fopen(argv[1], "r"); //using argv as a pointer to an array of pointers to arrays of characters
char *sentence = (char *) malloc(100 * sizeof(char)); //memory allocation, unsure what to use for size*


//fgets - reads a line from the specified stream and stores it into the string pointed to
//max amount set to 1000
while (fgets(sentence, 1000, file) != NULL) {
char *word;
int maxlen = 0;
char *maxW;
//max size of 1000 characters
maxW = (char *) calloc(1000, sizeof(char));
word = (char *) calloc(1000, sizeof(char));
word = strtok(sentence, " "); //using strtok to break sentance to token

//checking size of word with maxlen
while (word != NULL) {
if (strlen(word) > maxlen) {
maxlen = strlen(word);
strcpy(maxW, word);
}
word = strtok(NULL, " ");
}
printf("%s\n", maxW); //printing the max sized word
maxlen = 0; //reset

return 0;
}
}


I've only been using command line in windows to compile my code using gcc and I tried to use CLion but I can't figure out how to use CLion at the moment.

Edit: oops removed the image.

Answer

There are a few observations:

  • max size of sentence is smaller than max size of a word. Better use constants or macros to avoid typos like this.
  • You're using argv[1] (command line parameters) but nothing guarantees that the user will send a filename as parameter. Better validate this with a appropriate message (argc is the parameter count).
  • fopen tries to open the file, if it cannot do it, returns NULL, your code must do the verification.
  • The code allocates sentence, word and maxW for each line, but that's incorrect, so move these allocations to the top of main code.
  • word is just a pointer over sentence, so it doesn't need have memory allocated at all.
  • Your program only reads the first line and then stops, that's because it has a return 0; inside while.
  • Always free the memory that you allocates with malloc/calloc.

Apart from those observations, your code is almost there.

Reorganizing your code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Using constants avoid typos
#define MAX_SIZE    1000

int main(int argc, char** argv) {
    // Tokens not considered as part of a word
    const char tokens[] = " \t\n\r,/:[]=().<>";
    int maxlen = 0;
    // calloc(a,b) == malloc(a*b)
    char *sentence = (char *) malloc(MAX_SIZE * sizeof(char)); 
    char *maxW     = (char *) calloc(MAX_SIZE, sizeof(char));
    char *word;
    // try open and validate
    FILE *file     = fopen(argv[1], "rt"); 
    if(file == NULL){
        printf("file '%s' cannot be opened!\n", argv[1]);
        return 1; // !=0 means error to OS
    }

    while (fgets(sentence, MAX_SIZE, file) != NULL) {
        word = strtok(sentence, tokens);  
        while (word != NULL) {
            if (strlen(word) > maxlen) {
                maxlen = strlen(word);
                strcpy(maxW, word);
            }
            word = strtok(NULL, tokens);
        }
    }
    printf("Max word='%s' (len=%d)\n", maxW, maxlen); 

    // Don't forget free memory allocated with malloc/calloc
    free(sentence);
    free(maxW);

    return 0;
 }

And executing over the same program file:

$ gcc program.c
$ ./program program.c
Max word='considered' (len=10)
Comments