1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <stdio.h> char *fgets(char *s, int n, FILE *stream) { for (int i = 0; i < n; i++) { char c; fread(&c, 1, 1, stream); if (stream->has_error) { return NULL; } if (stream->is_eof) { return NULL; } s[i] = c; if (c == '\n') { break; } } return s; }