[comp.lang.c] fgets

libbyb@hpdml93.HP.COM (Libby Bagley) (08/30/89)

If I open a file using fopen(), I have the function
fgets() available to me.

If I open a file using open(), is there a function
similar to fgets() that I can use?  (Remember my
reference into the file is no longer a file
pointer, but an integer file descriptor.)

The question comes up because I would like to
lseek() a line at a time for variable length
lines.

Thanks for any help you can offer,

Libby Bagley

bright@Data-IO.COM (Walter Bright) (08/31/89)

In article <410002@hpdml93.HP.COM> libbyb@hpdml93.HP.COM (Libby Bagley) writes:
<The question comes up because I would like to
<lseek() a line at a time for variable length
<lines.

I don't know your specific application, but in general if you do that
you will wind up with an incredibly slow program. That's why the fxxxx()
functions were written, to buffer the disk I/O to speed it up.

I suggest you investigate using fseek() and ftell() instead.

ams@cbnewsl.ATT.COM (andrew.m.shaw) (08/31/89)

From article <410002@hpdml93.HP.COM>, by libbyb@hpdml93.HP.COM (Libby Bagley):
< If I open a file using fopen(), I have the function
< fgets() available to me.
< 
< If I open a file using open(), is there a function
< similar to fgets() that I can use?  (Remember my
< reference into the file is no longer a file
< pointer, but an integer file descriptor.)

FILE * fdopen (int fd, char * type) associates a stdio
stream with the fd returned by open(2).  This allows
you to use fgets,fseek, etc.  See fopen(3).

	Andrew Shaw

shankar@hpclscu.HP.COM (Shankar Unni) (09/01/89)

> The question comes up because I would like to
> lseek() a line at a time for variable length
> lines.

Try using "fdopen()" to attach a FILE buffer to the open file descriptor.
Then you can use "fgets", etc.
-----
Shankar Unni                                   E-Mail: 
Hewlett-Packard California Language Lab.     Internet: shankar@hpda.hp.com
Phone : (408) 447-5797                           UUCP: ...!hplabs!hpda!shankar

ydist@unix.cis.pitt.edu (Yijun Ding) (10/23/90)

After many tries to recompile my word processor by TC++, I find
the following difference:

fputs("", fp) return 0 in TC 2.0
              return (-1) in TC++

Which is correct ?

bhoughto@cmdnfs.intel.com (Blair P. Houghton) (10/23/90)

In article <52039@unix.cis.pitt.edu> ydist@unix.cis.pitt.edu (Yijun Ding) writes:
>After many tries to recompile my word processor by TC++, I find
>the following difference:
>
>fputs("", fp) return 0 in TC 2.0
>              return (-1) in TC++
>
>Which is correct ?

Under ANSI, the return value on error from fputs() is an EOF, the
return value if no error is "nonnegative".

In older versions (e.g.: on Apollos; and on Ultrix boxes,
which prove that "older" is not necessarily noncontemporaneous
with "current" :) there doesn't seem to be any mention of
fputs(3)'s having a return value.  It seems logical that
fputs() might have returned 0 on failure and nonzero on
success, such that `if ( fputs(...) )' might work.

In your case, the WP seems to depend on the "older" return
value.  Go through and see if you can find where they've
assigned or compared the return value of fputs() with anything.

try

	egrep '=.*fputs|fputs.*=' *.[ch]

in the source directory.

				--Blair
				  "...somma that old-time
				   a-rock and roll..."
				   -some Seeger

karl@haddock.ima.isc.com (Karl Heuer) (10/25/90)

In article <576@inews.intel.com> bhoughto@cmdnfs.intel.com (Blair P. Houghton) writes:
>In article <52039@unix.cis.pitt.edu> ydist@unix.cis.pitt.edu (Yijun Ding) writes:
>>fputs("", fp) return 0 in TC 2.0
>>              return (-1) in TC++
>>
>>Which is correct ?
>
>Under ANSI, the return value on error from fputs() is an EOF, the
>return value if no error is "nonnegative".  [Perhaps some nonstandard
>system is using 0 for failure.]

I don't think so.  It looks to me as though both implementations agree that -1
means failure, but it's questionable whether a zero-length write is a failure
or a trivial success.  (There are all sorts of dark corners when dealing with
zero-sized objects; the answer can be "operation fails", "operation succeeds",
or "behavior undefined" depending on the details.)  If the Standard isn't
clear on this (sorry, I can't look it up just now), one can always send X3J11
a request for interpretation.

The pragmatic solution is to rewrite the code so it doesn't matter:
	if (*s != '\0' && fputs(s, fp) == EOF) panic();

Karl W. Z. Heuer (karl@ima.isc.com or uunet!ima!karl), The Walking Lint