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.

Reply via email to