Strange behaviour of read work file

Hello all,

I got a csv-file from a foreign system and natural seems to read wrong values out of it. OK, I got a workaround for it. But I didn’t expect that behaviour.

Reproduceable sample :

define data local
1 #tab (A1/1:7)
end-define
define work file 1 '/tmp/test.csv' /* type 'csv'
write work file 1 variable 'A' 'B' 'C' 'D' 'E' 'F'
write work file 1 variable '1' '2' '3'
write work file 1 variable 'z' 'y' 'x' 'w'
write work file 1 variable 'a' 'b' 'c'
close work file 1
read work file 1 #tab(*)
write            #tab(*)
end-work
end

Output:


A B C D E F
1 2 3 D E F
z y x w E F
a b c w E F

… a trap for the unwary. Since there is no “read work … variable” my workaround is:


read work file 1 #TAB(*) giving length #csvfields
if #csvfields <  *UBOUND(#TAB)
add 1 to #csvfields
reset #tab(#csvfields:7)
end-if

Regards

matthias

This may not be the same issue… but you need to reset the array, as you are not reading values into all the fields each time

define data local
1 #tab (A1/1:7)
end-define
define work file 1 ‘/tmp/test.csv’ /* type ‘csv’
write work file 1 variable ‘A’ ‘B’ ‘C’ ‘D’ ‘E’ ‘F’
write work file 1 variable ‘1’ ‘2’ ‘3’
write work file 1 variable ‘z’ ‘y’ ‘x’ ‘w’
write work file 1 variable ‘a’ ‘b’ ‘c’
close work file 1
reset #tab() / reset array… do not wish to have OLD data
read work file 1 #tab()
write #tab(
)
reset #tab() / reset array… do not wish to have OLD data
end-work
end

The behavior is documented, but I’ve never experienced it because I always using a single WRITE WORK for my detail records, so all detail records have the same number of fields.
The SELECT option is the default for a READ WORK.

Thanks for answering.

@James Gilpin: Yes, it is the same issue and your RESET is a valid Workaround, too. I prefer to do a RESET at the beginning of the loop - just in case there are some ESCAPE TOPs in the following code.

@Ralph Zbrog: In my case, a non-Natural-Application writes the workfile… So I wasn’t aware of the individual length of the records.