0

I'm working on some homework and one of the functions I need to write should look through a text file that represents a dictionary and update a struct with the number of words I found, the shortest word, and the longest word.

However, when I try to run the program, it just hangs out in command prompt for a while as if I'm in a infinite loop.

int info(const char *dictionary, struct DICTIONARY_INFO *dinfo)
{
  /* Variable Declarations                                                 */
  FILE *fp;                     /* Pointer to the file being opened        */
  char str[21] = {0};           /* Char array to hold fgets                */
  unsigned int longest_length;  /* Length of the longest word in the dict  */
  unsigned int shortest_length; /* Length of the shorest word in the dict  */
  int number_words;             /* Number of words in dict                 */

  longest_length = 0;
  shortest_length = 21; 
  number_words = 0;

  /* Opens the file                                                        */
  fp = fopen(dictionary, "rt");

  /* If the file was successfully opened                                   */
  if(fp)
  {
    /* While we're not at the end of the file                              */
    while(!feof(fp))
    {
      /* If we can successfully get a line with fgets                      */
      if(fgets(str, strlen(str), fp))
      {
        /* Replaces the NL char with a null 0.                             */
        my_nl_replace(str);

        /* If the length of this word is longer than our current longest   */
        if(strlen(str) > longest_length)
          longest_length = strlen(str);

        /* If the length of this word is shorter than the current shortest */
        if(strlen(str) < shortest_length)
          shortest_length = strlen(str);

        ++number_words;
      }
    }

    /* Closes the file, since we're done using it                          */
    fclose(fp);

    /* Modifies the dictionary struct with the info just collected         */
    (*dinfo).shortest = shortest_length;
    (*dinfo).longest = longest_length;
    (*dinfo).count = number_words;
    return FILE_OK; 
  }

  /* If the file was not successfully opened                               */
  else
  {
    return FILE_ERR_OPEN;
  }
}

And if anyone's curious, the nl_reaplace thing is as follows:

void my_nl_replace(char *string)
{
  string[(strlen(string))] = '\0';
}
red_always_mafia
  • 179
  • 2
  • 10
  • 2
    `if(fgets(str, strlen(str), fp))` --> `if(fgets(str, sizeof(str), fp))` – BLUEPIXY Nov 22 '14 at 18:48
  • 1
    Also see this: http://stackoverflow.com/q/5431941/694576 – alk Nov 22 '14 at 18:49
  • Ah ok. I'm given that it ends in a new line so I probably find it that way. But I don't think that is my current problem because I placed some printf statements throughout the function and it didn't get to the point where it actually calls nl replace. It doesnt really go anywhere as of now. – red_always_mafia Nov 22 '14 at 18:50
  • [Please don't use `feof`.](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong) Make the return value of `fgets` the condition for your loop. Your replacement function doesn't anything to null-terminating strings and won't work for char buffers that are not null-terminated. – M Oehm Nov 22 '14 at 18:51
  • Yeah another user pointed out the replace issue, I'm fixing that now. I will try Using fgets instead of feof for the loop and see if that works. – red_always_mafia Nov 22 '14 at 18:52
  • Not a solution but will help you find it: GDB (or an other debugger) learn to use it and debug your program step by step. – Karl Nov 22 '14 at 18:53

1 Answers1

1

Where you write strlen(str) it translates to zero because str is initialized to start with zeroes and as such is an empty string. You will repeatedly read zero characters and since you never read any characters from the file, you will never reach EOF, so you will loop forever.

What you should do is save the current position in the file, read a certain number of characters (in your case 21), read the length of the first word from the buffer and reset the position to where it was previously, plus the length of the word you just read. For how to do this see fseek().

asm
  • 8,758
  • 3
  • 27
  • 48
user2008934
  • 321
  • 2
  • 9