I am doing pset2 hacker edition. I am trying to read a line of string from "/usr/share/dict/words" as per pset specification using fgets() function but instead of reading the full line it is only reading first three characters of the string & adding an extra newline in the end of string. Please help me how can I read a full line of string. Also I'd like to say I've tried using getline() function but with the header file #include which is compulsory to invoke crypt() function its header files are ignored or something else I don't know & is giving me the error implicit function declaration of grtline() function.

  • Can you show the declaration of the string you are using to store the readed line, and the call of the fgets()?, the problem may be there.
    – wallek876
    Commented Jan 12, 2015 at 11:26
  • Other useful function for this case can be fscanf().
    – wallek876
    Commented Jan 12, 2015 at 11:27
  • this is the snippet of my code.char* word = malloc(sizeof(char*) * 1); // read the text from file "usr/share/dict/words" for (int i = 1; fgets(word, sizeof(word), fp) != NULL; i++) Commented Jan 12, 2015 at 11:35

1 Answer 1


First consider the size this elements have in the cs50 appliance.

  • char = 1byte
  • char* (pointer) = 4bytes

You are declaring word and initializing it with the pointer returned by malloc, that is fine. But you are allocating:

malloc(sizeof(char*) * 1);

You need to realize that the sizeof(char*) is the size of a pointer, which is 4bytes in the cs50 appliance, and the same for the call to fgets:

fgets(word, sizeof(word), fp)

the second argument of fgets() specifies the maximum number of characters to be read from the stream including the null character. So you are reading the sizeof(word) which is 4 per the declaration of word, 3 characters plus the null character.

You need to initialize word with enough space to store the longest word in the dictionary plus the null character, and then you can read the same amount in fgets().

  • thanx the explanation is satisfactory Commented Jan 12, 2015 at 12:48
  • For crack.c purposes you don't need to make word too big, as maximum password length is 8 characters Commented Feb 1, 2015 at 17:45
  • @VladimirKlimenko what he or she is trying is to read lines or words from a file, that can have more than 8 characters. But you are completely right, from the strings with more than 8 characters you only need the first 8. In fact if you try to encrypt these passwords, 12345678 and 12345678abcd with the same salt just as an example, the crypt() function used in the pset is going to produce the same hash for both because it only uses the first eight to produce the hash. For this case it doesn't matter if you try passwords with more than eight characters.
    – wallek876
    Commented Feb 1, 2015 at 21:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .