In pset6 specification, in question 5, we have been asked why fgetc has been used instead of fscanf. And i don't understand the benefit of using fgetc. So please explain this.

Also, in pset 6 walkthroughs with Zamyla, in the speller load walkthrough,at around 3:46, she also shows fcanf itself. In that slide, she shows

node* new_node = malloc(sizeof(node));
fscanf(file, "%s", new_node->word);

To practice for this pset, I implemented a linked list and a hash table but I can't seem to understand how to start this pset. Basically I am trying to load into a char array, the words from the dictionary one by one. Whenever i meet a '\n' character it means that the old word is finished and so I try to insert that word into the hash table and then continue reading from dictionary. Am i in the right direction?

2 Answers 2


If you remember from previous weeks, fscanf() by itself is actually not the best function to use to get input specifically strings.

fgetc() reads character-by-character from a stream. This will enable us to keep track of the number of chars we read, while using fscanf(), on the other hand, might get us into trouble if a longer string than we expect was read.

This might overwrite other important data in memory or probably makes our program exist with a segmentation fault.

  • scanf?? did you mean fscanf?? And all the words in the dictionary have a max length of 45. And all are terminated by \n Jul 6, 2014 at 12:23
  • @MonkeyD.Luffy they're basically the same except that scanf() reads from the standard input (e.g., the keyboard) while fscanf() reads from a stream (e.g., a file). And yes, we know that the maximum length of a word in the default dictionary is 45 characters long, but what if another dictionary is used? Doesn't your program accept an optional command-line argument for a dictionary path?
    – kzidane
    Jul 6, 2014 at 12:53
  • using fscanf will not create a problem in case of words with apostrophes? Jul 6, 2014 at 13:23
  • @user1400 an apostrophe is just a character like any other characters. What makes you think that fscanf() might have problems with apostrophes?
    – kzidane
    Jul 6, 2014 at 14:09
  • 1
    Apostrophes won't pose a problem. However, we want better control, because no punctuation will upset fscanf, and it will plod along picking up commas and quotation marks as well as alphanumeric chars, which is not what we want.
    – postylem
    Sep 8, 2014 at 12:10

fscanf() should be okay for reading in from the dictionary file, because the words are all separated by line returns. But, fscanf() could cause problems if used to read in from the text file because words in that file will not always end with a space or line return. Many will be followed by a period or comma, for example, which fscanf will see as being part of the word.

You must log in to answer this question.

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