Incomplete final line found - what's the big deal?

Occasionally, when I use the readLines() functions I get the following warning message:

readLines("XXX") : incomplete final line found on 'XXX'

To fix this warning message is straight forward, just add a newline at the end of the file.

My questions are:

  • Why is this an issue?
  • What are the (historical?) reasons behind this warning?
  • Is it good practice to promote this warning to an error?

Note: RStudio has an option that automatically adds a line breaks: Code -> Saving

1 Like

Good question.

What does a function like readLines() do?

In OS terms it reads from stdin and writes to stdout, a line at a time. So, it eats a line and then looks for the next line. But what if there is no next line? It could either say, "fine, I'll go with what I got," or "guess I'll have to wait until I'm fed," or "better tell the user I need to know when I'm done."

This isn't a problem on the OS terminal because the operating system has the go-with-that choice baked in. But in character devices, like a file, the C/C++ program needs to be told.

man getc

from the terminal will give you the gory details.


Thanks! Your answer implies that it is good practice to promote this warning to an error. Do you agree (or am I putting words into your mouth)?

1 Like

Sorry to be implicit when I should have been explicit.

Yes, the error is needed. It should be more informative such as "... add an empty line to the end of your file."

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.