version 1.13, 2004/04/21 00:21:04 |
version 1.14, 2004/05/10 17:15:28 |
|
|
.Sh SEE ALSO |
.Sh SEE ALSO |
.Xr ferror 3 , |
.Xr ferror 3 , |
.Xr fgets 3 , |
.Xr fgets 3 , |
.Xr fgetstr 3 , |
|
.Xr fopen 3 , |
.Xr fopen 3 , |
.Xr putc 3 |
.Xr putc 3 |
.Sh HISTORY |
.Sh HISTORY |
|
|
.Fn fgetln |
.Fn fgetln |
function first appeared in |
function first appeared in |
.Bx 4.4 . |
.Bx 4.4 . |
.Sh NOTES |
.Sh CAVEATS |
The |
Since the returned buffer is not a C string (it is not null terminated), a |
.Fn fgetln |
common practice is to replace the newline character with |
function is equivalent to |
.Sq \e0 . |
.Fn fgetstr |
However, if the last line in a file does not contain a newline, |
with a \en |
the returned text won't contain a newline either. |
.Fa delim . |
The following code demonstrates how to deal with this problem by allocating a |
|
temporary buffer: |
|
.Bd -literal |
|
char *buf, *lbuf; |
|
size_t len; |
|
|
|
lbuf = NULL; |
|
while ((buf = fgetln(fp, &len))) { |
|
if (buf[len - 1] == '\en') |
|
buf[len - 1] = '\e0'; |
|
else { |
|
if ((lbuf = (char *)malloc(len + 1)) == NULL) |
|
err(1, NULL); |
|
memcpy(lbuf, buf, len); |
|
lbuf[len] = '\e0'; |
|
buf = lbuf; |
|
} |
|
printf("%s\en", buf); |
|
|
|
if (lbuf != NULL) { |
|
free(lbuf); |
|
lbuf = NULL; |
|
} |
|
} |
|
.Ed |