J
John Reye
Hello,
The last character read from fgets(buf, sizeof(buf), inputstream) is:
'\n'
OR
any character x, when no '\n' was encountered in sizeof(buf)-1
consecutive chars, or when x is the last char of the inputstream
***How can one EFFICIENTLY determine if the last character is '\n'??
"Efficiently" means: don't use strlen!!!
I only come up with the strlen method, which - to me - says that fgets
has a bad design.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buf[6];
FILE *fp = stdin;
while (fgets(buf, sizeof(buf), fp)) {
printf((buf[strlen(buf)-1] == '\n') ? "Got a line which ends with
newline: %s" : "no newline: %s", buf);
}
return EXIT_SUCCESS;
}
A well-designed fgets function should return the length of characters
read, should it not??
Please surprise me, that there is a way of efficiently determining the
number of characters read.
I've thought of ftell, but I think that does not work with stdin.
Because right now, I think that fgets really seems useless.
Why is the standard C library so inefficient?
Do I really have to go about designing my own library?
Thanks for tipps and pointers
Regards,
J.
The last character read from fgets(buf, sizeof(buf), inputstream) is:
'\n'
OR
any character x, when no '\n' was encountered in sizeof(buf)-1
consecutive chars, or when x is the last char of the inputstream
***How can one EFFICIENTLY determine if the last character is '\n'??
"Efficiently" means: don't use strlen!!!
I only come up with the strlen method, which - to me - says that fgets
has a bad design.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char buf[6];
FILE *fp = stdin;
while (fgets(buf, sizeof(buf), fp)) {
printf((buf[strlen(buf)-1] == '\n') ? "Got a line which ends with
newline: %s" : "no newline: %s", buf);
}
return EXIT_SUCCESS;
}
A well-designed fgets function should return the length of characters
read, should it not??
Please surprise me, that there is a way of efficiently determining the
number of characters read.
I've thought of ftell, but I think that does not work with stdin.
Because right now, I think that fgets really seems useless.
Why is the standard C library so inefficient?
Do I really have to go about designing my own library?
Thanks for tipps and pointers
Regards,
J.