I was reading the manual page for the getline
function and saw a demonstration of it :
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t nread;
...
while ((nread = getline(&line, &len, stream)) != -1) {
printf("Retrieved line of length %zu:
", nread);
fwrite(line, nread, 1, stdout); /* ? */
}
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
}
I replaced the fwrite()
statement with printf ("%s", line)
and it produced
identical results (compared using cmp
and diff
). I am aware of the distinction between fwrite
and fprint
but was there any specifc reason the author chose to use fwrite()
over fprintf
or printf
in this context ?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…