UMBC CMSC 201 Spring '02 | CSEE | 201 | 201 S'02 | 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 integers
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, so reading in strings using fgets() and putting newlines between each string when recreating the file is guaranteed to be the same as the original.