Since the Standard C committee did not standardize a simple replacement for gets(), what should it be?

前端 未结 3 575
情歌与酒
情歌与酒 2021-01-04 00:22

The gets function was first deprecated in C99 and finally removed in C11. Yet there is no direct replacement for it in the C library.

fgets()

3条回答
  •  一整个雨季
    2021-01-04 01:14

    This question largely calls for speculation short of a citation from committee minutes or something, but as a general principle, the committee (WG14) generally avoids inventing new interfaces and prefers to document and make rigorous existing practice (things like snprintf, long long, the inttypes.h types, etc.) and sometimes adopt from other standards/interface definitions outside of C (e.g. complex math from IEEE floating point, atomic model from C++, etc.). gets has no such replacement to adopt, probably because fgets is generally considered superior (it's non-lossy when the file ends without a newline). If you really want a direct replacement, something like this works:

    char buf[100];
    scanf("%99[^\n]%*1[\n]", buf);
    

    Of course it's klunky to use, especially when the buffer size is variable.

提交回复
热议问题