C fgets versus fgetc for reading line

后端 未结 5 2173
南笙
南笙 2020-12-30 09:05

I need to read a line of text (terminated by a newline) without making assumptions about the length. So I now face to possibilities:

  • Use fgets and
5条回答
  •  北海茫月
    2020-12-30 09:36

    If you can set a maximum line length, even a large one, then one fgets would do the trick. If not, multiple fgets calls will still be faster than multiple fgetc calls because the overhead of the latter will be greater.

    A better answer, though, is that it's not worth worrying about the performance difference until and unless you have to. If fgetc is fast enough, what does it matter?

提交回复
热议问题