UMBC CMSC 201
CSEE | 201 | 201 F'06 | lectures | news | help
For example, suppose you are finding the areas of a bunch of rectangles. The data to be read in could logically be a file with two ints on a line. Reading in two ints at a time with fscanf() makes a lot more sense than using fgets() and writing complicated code to split up the string and then change each part into an integer.
Sometimes it isn't a matter of a bad choice meaning more complicated code. Sometimes things that you think would work just won't. For example, if you want to copy a file full of text, using fscanf() with %s to read in the file one word at a time will not be able to recreate the file. Since fscanf() will break at any whitespace, including tabs and newlines, recreating the file by spitting out the strings with spaces in between them will make one long line of text that wraps off the screen.
However, fgets only stops at a newline character and includes the newline character in the string. So reading in strings using fgets() will let you recreate the file and it will be the same as the original.