Re: Question regarding fgets and new lines



Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx> writes:
Keith Thompson wrote:
[...]
Well, you *could* return as many unique error conditions as you like
with a simple char* return value:
const char *const gl_EOF = "getline: EOF";
const char *const gl_io_error = "getline: I/O error";
const char *const gl_malloc_failed = "getline: malloc failed";
...
[...]
Different people design different interfaces for the same
task! Different people decompose the same task in different ways!
Ultimately, it comes down to what might be called "taste" (there's
just no point in arguing with Gus), or to put it on a more respectable
footing it comes down to a guess about the likely usage scenarios for
the new facility. There might (or might not) be the germ of a thesis
topic for someone who wants to make a study of how different
programmers approach similar problems: were you corrupted by an early
exposure to
Forth, was your mother frightened by a COBOL compiler?
[...]

Thinking about it a bit more, I don't necessarily *like* the interface
I came up with. If you really really want to have your function
return just a single char* value and still be able to represent
multiple error conditions, what I suggested is probably close to the
best way to do it. The language provides one distinguished pointer
value that doesn't point to actual data; gl_EOF, gl_io_error, et al
can be thought of as multiple null pointers. But returning the actual
data and a status code separately is, IMHO, better style.

It would be nice if a function could return multiple values, so you
could do something like:

char *line;
int status;
(line, status) = getline(stdin);

But that's not C. Yes, getline() could return a struct (and using a
struct might not be a horrible idea), but using a struct probably
isn't as convenient as multiple return values would be.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages