Re: Question regarding fgets and new lines



CBFalconer <cbfalconer@xxxxxxxxx> writes:
Eric Sosman wrote:

... snip ...

FWIW, I took a somewhat different tack in my own gets()
replacement (I guess everybody writes one, sooner or later).
Mine follows the precedent of things like getenv(): the returned
pointer is only valid until the next call, when the buffer it
points to may be overwritten and/or moved or freed.

This approach has some disadvantages: for example, it would
be a pain to make it thread-friendly. On the other hand, it
localizes all the memory management inside the function, and the
signature `char *getline(FILE*)' is simple enough that even I can
remember it. (The older and feebler my gray cells get, the more
I value simplicity ...)

When I designed ggets I considered that signature, but I could see
no way of returning appropriate errors for both FILE problems, EOF,
and memory allocation problems.

I second the motion about easily remembered signatures.

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";
...

Each error code is a unique pointer value. One problem is that
there's no good way to detect success other than by comparing the
result to each possible error code (a set that can easily change in
later revisions of the function); this can be alleviated somewhat by
putting all the error codes into an array, but it's still inconvenient
for the user. You could provide an auxiliary function that tells you
whether the char* value returned by getline() points to an error
message or not.

Another drawback is that you can't return both error information and
actual data.

But if the user forgets to check the result, the value returned looks
like an error message which could make it easier to track down bugs.

Another approach is to implement something resembling errno, but that
can make it difficult to tie an error to a specific call.

--
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

  • Re: Cannot marshal return value
    ... type of your method signature and change the return type from char[] to ... Shell MegaPack: GUI Controls For Drop-In Windows Explorer like Shell ... This is likely because the managed PInvoke signature ... public static extern IntPtr DiskSaveFooter(string missionname, ...
    (microsoft.public.dotnet.framework.interop)
  • Re: [head tinderbox] failure on arm/arm
    ... *** Error code 1 ... when compiled on FreeBSD/arm, causes a -Wcast-align warning ... You can't cast any random (char *) pointer to a pointer of a type which ...
    (freebsd-current)
  • Re: Error 800B0100 with Update KB938123
    ... No root certificate authority can verify the signature ... If Vista is not in English, than use the "Change Language" drop down window to obtain the language. ... IF KB938123 installs, than restart the system if requested to. ... Unable to find additional info will searching error code. ...
    (microsoft.public.windowsupdate)
  • Re: more 500 Servlet Exception
    ... What is happening here is that I am trying to run this PL/SQL script ... that contains a signature. ... servlet that checks it for validity and what not. ... then call the database and returns an error code if needed. ...
    (comp.lang.java.programmer)
  • Re: Character line break
    ... try the cool overloaded String constructor of signature: ... ..ctor (char c, int count) ...
    (microsoft.public.dotnet.languages.csharp)