Re: [dev] print utility

From: Charlie Kester <corky1951_AT_comcast.net>
Date: Sun, 31 Mar 2013 06:52:13 -0700

On 03/30/2013 19:27, Calvin Morrison wrote:
>
> Is there a better way to read a whole line? fgets requires a length
> argument, so I suppose it's not a very good solution

Framing the problem that way is where you went astray.

I'd read the file one character at a time, counting newlines, until I
reached the desired line.

Then I'd continue reading a character at a time, echoing each character
to stdout, until I reached the end of the line. (After which, as Robert
pointed out, there's no reason to continue proocessing the file.)

Doing it this way avoids the need for a buffer altogether, along with
any guessing about possible line lengths.

You were probably thinking that reading a line at a time would be more
efficient. But your intuition is mistaken.
Received on Sun Mar 31 2013 - 15:52:13 CEST

This archive was generated by hypermail 2.3.0 : Sun Mar 31 2013 - 15:48:02 CEST