DelimitedFiles.jl icon indicating copy to clipboard operation
DelimitedFiles.jl copied to clipboard

readdlm() cannot process empty file

Open innerlee opened this issue 9 years ago • 7 comments

  • julia ver: 0.4.5
  • os ver: win 10

reproduce:

julia> writedlm("emp.ty", [])
julia> readdlm("emp.ty")
ERROR: at row 0, column 0 : ArgumentError("number of rows in dims must be > 0, got 0")
 in error at error.jl:21

Is this behavior by design?

innerlee avatar May 07 '16 13:05 innerlee

It appears to be by design given that there's an explicit error for it. What behavior would you have expected? A 0x0 array?

ararslan avatar May 07 '16 19:05 ararslan

Cc @tanmaykm

ViralBShah avatar May 08 '16 05:05 ViralBShah

The problem is that readdlm can't know the number of columns of the result. So if in general your code works with two-columns arrays, a 0x0 array will likely raise an error later anyways. By throwing the error immediately, readdlm allows you to create a matrix of the correct size.

We could provide a way to specify the number of columns, and not raise an error if it's set. Currently AFAIK dims cannot be used for that.

nalimilan avatar May 08 '16 16:05 nalimilan

I think 0x0 array is okay, because

  • I do not agree that a 0x0 array is useless, or equals an error.
  • Columns can vary, like rows. I mean, one might read, append columns to the data, and rewrite the file.
  • When accessing data, one checks the bounds, like in for i=1:length(sth). So, 0x0 will be safe in this case.
  • Its easier to check size than catch exceptions.

innerlee avatar May 12 '16 12:05 innerlee

0x0 array was definitely was definitely the behaviour that I expected, else what it returns in this case could be specified with a keyword (like :throwifempty or something?)

alexmorley avatar May 30 '16 12:05 alexmorley

a 0x0 array is also what i expected

bjarthur avatar Feb 13 '22 13:02 bjarthur

It seems like empty files might indicate errors, but no less so than arbitrarily truncated files (which we cannot detect). Do you want to make a PR to return a 0x0 Any array?

vtjnash avatar Feb 13 '22 20:02 vtjnash