Using fscanf() vs. fgets() and sscanf()

后端 未结 3 1788
再見小時候
再見小時候 2020-11-29 06:23

In the book Practical C Programming, I find that the combination of fgets() and sscanf() is used to read input. However, it appears to me that the

3条回答
  •  误落风尘
    2020-11-29 06:43

    There is no difference between fscanf() versus fgets()/sscanf() when:

    1. Input data is well-formed.

    Two types of errors occur: I/O and format. fscanf() simultaneously handles these two error types in one function but offer few recovery options. The separate fgets() and sscanf() allow logical separation of I/O issues from format ones and thus better recovery.

    1. Only 1 parsing path.

    Separating I/O from scanning allows multiple sscanf() options. Should a given scanning of a buffer not realize the desired results, other sscanf() with different formats are available.

    1. No embedded '\0'.

    Rarely does '\0' occurs, but should one occur, sscanf() will not see it as scanning stops with its occurrence, whereas fscanf() continues.

    In all cases, check results of all three functions.

提交回复
热议问题