c - Why does fgets() return a pointer to the newly filled char array? -
i can use fgets in following way:
file *fp = fopen("text.txt", "r"); if(fp == null) return 1; char line[50]; fgets(line, 50, fp); // more stuff
this fills line
expected chars file, why fgets()
need return pointer line
? line
contains data needed use returning pointer it?
because documented in fgets(3) , in posix fgets so.
also, if sure return non-null
pointer (i.e. not fail), might use result argument other function. think mistake not test it.
btw, recommend using newer posix getline able deal arbitrarily long lines (as long resources permit it) -and returning on success line length.
Comments
Post a Comment